Created
February 18, 2021 23:02
-
-
Save kucaahbe/131c265f25bf7979ae4e9e92f65f2982 to your computer and use it in GitHub Desktop.
lua benchmark
This file contains hidden or 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 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 | |
} |
This file contains hidden or 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 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