profiling lua

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

profiling lua

Adrian Sietsma
some fun with debug hooks


local f0 = function() end
local f1 = function() return math.sin(math.cos(math.random())) end
local n, n1 = 0

debug.sethook(function() n=n+1 end,"",1) -- hook every instruct
n=0
f0()
n1=n
print("f0()",n1-1)

n=0
f0(3)
n1=n
print("f0(3)",n1-1)

n=0
f1()
n1=n
print("f1()",n1-1)

n=0
local t = f1(23)
n1=n
print("t=f1(23)",n1-1)

this allows you to count lua instructions for any given code path.
it's not a cpu time model, but it's interesting to see the results of code changes, and you don't need to loop to get a meaningful result, unlike trying to time a function.

caveat - this only counts lua instructions - a 10-deep metatable lookup still counts as one instruction.

Adrian