Skip to content

Instantly share code, notes, and snippets.

@jonasgeiler
Last active April 15, 2023 14:00
Show Gist options
  • Save jonasgeiler/42f1d09529ec4f2b3f3b897c27c9402f to your computer and use it in GitHub Desktop.
Save jonasgeiler/42f1d09529ec4f2b3f3b897c27c9402f to your computer and use it in GitHub Desktop.
A super simple benchmark function in Lua
---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