Last active
April 13, 2023 11:52
-
-
Save tylerneylon/ecb44dc54a78cd955c68 to your computer and use it in GitHub Desktop.
measure Lua code speed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a super-fast kinda-hacky way to measure the speed of a section of Lua code.
Call
mark1
,mark2
, andmark3
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 betweenmark1
andmark3
.