Skip to content

Instantly share code, notes, and snippets.

@tylerneylon
Last active April 13, 2023 11:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tylerneylon/ecb44dc54a78cd955c68 to your computer and use it in GitHub Desktop.
Save tylerneylon/ecb44dc54a78cd955c68 to your computer and use it in GitHub Desktop.
measure Lua code speed
local t_sum = 0
local t_num = 0
local t1, t2, t3
local do_time = true
local function mark1()
if not do_time then return end
t1 = os.clock()
end
local function mark2()
if not do_time then return end
t2 = os.clock()
end
local function mark3()
if not do_time then return end
t3 = os.clock()
print('---')
print(string.format('t1-t3 took %gs', t3 - t1))
print(string.format('t1-t2 took %gs', t2 - t1))
print(string.format('t2-t3 took %gs', t3 - t2))
t_sum = t_sum + t3 - t1
t_num = t_num + 1
print(string.format('avg t1-t3 is now %gs', t_sum / t_num))
end
@tylerneylon
Copy link
Author

This is a super-fast kinda-hacky way to measure the speed of a section of Lua code.

Call mark1, mark2, and mark3 in order. The intended use case is to either isolate a bottleneck - decided between the first or second interval measured - or to get an absolute measure of the average run time of the code between mark1 and mark3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment