fix bench basics

This commit is contained in:
BlackMATov
2025-01-06 05:32:46 +07:00
parent 1ebeb5d84b
commit 50e4372311

View File

@@ -14,21 +14,31 @@ end)()
---@param loop fun(...): ... ---@param loop fun(...): ...
---@param init? fun(): ... ---@param init? fun(): ...
function basics.describe_bench(name, loop, init) function basics.describe_bench(name, loop, init)
collectgarbage('collect')
collectgarbage('stop')
print(string.format('| %s ... |', name)) print(string.format('| %s ... |', name))
local iters = 0
local state = init and __table_pack(init()) or {} local state = init and __table_pack(init()) or {}
pcall(function() do
local warmup_s = os.clock() local warmup_s = os.clock()
local success, result = pcall(function()
repeat repeat
loop(__table_unpack(state)) loop(__table_unpack(state))
until os.clock() - warmup_s > 0.1 until os.clock() - warmup_s > 0.1
end) end)
if not success then
print('|-- FAIL: ' .. result)
return
end
end
collectgarbage('collect')
collectgarbage('stop')
do
local iters = 0
local start_s = os.clock() local start_s = os.clock()
local start_kb = collectgarbage('count') local start_kb = collectgarbage('count')
@@ -42,14 +52,16 @@ function basics.describe_bench(name, loop, init)
local finish_s = os.clock() local finish_s = os.clock()
local finish_kb = collectgarbage('count') local finish_kb = collectgarbage('count')
print(string.format(' %s | us: %.2f | op/s: %.2f | kb/i: %.2f | iters: %d', if success then
success and 'PASS' or 'FAIL', print(string.format('|-- PASS | us: %.2f | op/s: %.2f | kb/i: %.2f | iters: %d',
(finish_s - start_s) * 1e6 / iters, (finish_s - start_s) * 1e6 / iters,
iters / (finish_s - start_s), iters / (finish_s - start_s),
(finish_kb - start_kb) / iters, (finish_kb - start_kb) / iters,
iters)) iters))
else
if not success then print(' ' .. result) end print('|-- FAIL: ' .. result)
end
end
collectgarbage('restart') collectgarbage('restart')
collectgarbage('collect') collectgarbage('collect')