Skip to content

Instantly share code, notes, and snippets.

@SamMousa
Created August 21, 2022 18:25
Show Gist options
  • Save SamMousa/a0c44317833540d2172033b9c2b02e2f to your computer and use it in GitHub Desktop.
Save SamMousa/a0c44317833540d2172033b9c2b02e2f to your computer and use it in GitHub Desktop.
LUA profiling in wow
local prefix = 'profileTest'
local value = math.random()
local function testFunction(a, b)
return a + b
end
local function differences(t, start)
local result = {}
local prev = start
for _, v in ipairs(t) do
result[#result + 1] = v - prev
prev = v
-- print(result[#result])
end
return result
end
local function stats(t)
local min = t[1]
local max = t[1]
local sum = 0
for _, v in ipairs(t) do -- Get the sum of all numbers in t
sum = sum + v
if v > max then
max = v
end
if v < min then
min = v
end
end
return min, max, sum / #t
end
local function profile(name, callback, iterations)
iterations = iterations or 100
local times = {}
print(string.format(">> START PROFILING, %d ITERATIONS", iterations));
print(name);
local start = debugprofilestop()
for i = 1, iterations do
callback()
times[i] = debugprofilestop()
end
-- output
print(string.format("It took %d / %d / %d (min/max/avg) ms", stats(differences(times, start))))
-- print("<< END PROFILING");
end
function initializeGlobals()
profile("Initializing ~100.000 globals", function()
_G["ProfileTestFunction"] = testFunction
-- for i = 0, 100000 do
-- _G[prefix .. i] = value
-- end
end)
end
function doProfiling1()
profile("Start profiling global accessor 100.000", function()
local sum = 0;
for i = 0, 100000 do
sum = ProfileTestFunction(sum, 1)
end
end);
end
function doProfiling2()
local g = function(a, b) return ProfileTestFunction(a, b) end
local g1 = function(a, b) return g(a, b) end
local g2 = function(a, b) return g1(a, b) end
profile("Start profiling global accessor, nested 3 times, 100.000", function()
local sum = 0;
for i = 0, 100000 do
sum = g2(sum, 1)
end
end);
end
function doProfiling3()
profile("Start profiling local accessor 100.000", function()
local sum = 0;
for i = 0, 100000 do
sum = testFunction(sum, 1)
end
end);
end
local l = function(a, b) return testFunction(a, b) end
local l1 = function(a, b) return l(a, b) end
local l2 = function(a, b) return l1(a, b) end
function doProfiling4()
profile("Start profiling local accessor, nested 3 times, 100.000", function()
local sum = 0;
for i = 0, 100000 do
sum = l2(sum, 1)
end
end);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment