Last active
April 15, 2023 14:00
-
-
Save jonasgeiler/42f1d09529ec4f2b3f3b897c27c9402f to your computer and use it in GitHub Desktop.
A super simple benchmark function in Lua
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
---Run a benchmark | |
---@param fn function | |
---@param outer_iter integer? | |
---@param inner_iter integer? | |
local function bench(fn, outer_iter, inner_iter) | |
outer_iter = outer_iter or 100 | |
inner_iter = inner_iter or 10000 | |
local times = {} ---@type number[] | |
for i = 1, outer_iter do | |
local from = os.clock() | |
for j = 1, inner_iter do | |
fn() | |
end | |
local to = os.clock() | |
times[#times + 1] = to - from | |
end | |
local sum = 0 | |
for i = 1, #times do | |
sum = sum + times[i] | |
end | |
print('average seconds:', sum / #times) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment