Skip to content

Instantly share code, notes, and snippets.

@kucaahbe
Created February 18, 2021 23:02
Show Gist options
  • Save kucaahbe/131c265f25bf7979ae4e9e92f65f2982 to your computer and use it in GitHub Desktop.
Save kucaahbe/131c265f25bf7979ae4e9e92f65f2982 to your computer and use it in GitHub Desktop.
lua benchmark
local function run(self, name, f)
local start = os.clock()
f()
local now = os.clock()
table.insert(self.result, { name=name, time=now-start })
end
local function report(self)
local data = ''
for i, rep in ipairs(self.result) do
data = data..string.format('%-15s\t%8.6f', rep.name, rep.time)
if i ~= #self.result then data = data..'\n' end
end
return data
end
return {
new = function ()
return {
run = run,
report = report,
result = {},
}
end
}
local benchmark = require('tiny_benchmark')
local N = 1e8
bm = benchmark.new()
bm:run("stuff A", function ()
for i=0, N do
-- do stuff A
end
end)
bm:run("stuff B", function ()
for i=0, N do
-- do stuff B
end
end)
print(bm:report())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment