Skip to content

Instantly share code, notes, and snippets.

@ochaton
Created May 27, 2020 16:28
Show Gist options
  • Save ochaton/bbe4165c9374f2e5e9fc118718bc30e2 to your computer and use it in GitHub Desktop.
Save ochaton/bbe4165c9374f2e5e9fc118718bc30e2 to your computer and use it in GitHub Desktop.
helpers to consume CPU for specific amount of time
local clock = require 'clock'.time
function getms1()
local bench = function(f, ...)
local tail = function(s, ...)
return clock()-s, ...
end
local s = clock()
return tail(s, f(...))
end
local function loop(n) local x = 0 for i = 1, n do x = x + 1 end end
-- initial value:
local x = 100*1000
local ms1 = 0.001
for i = 1, 100 do
local lat = bench(loop, x)
x = x*ms1/lat
end
local ms10 = 10*ms1
for i = 1, 10 do
local lat_10 = bench(loop, 10*x)
x = x*ms10/lat_10
end
return function() return loop(x) end, x
end
longfunc = function(st)
local time = clock.time
local s = time()
local c = st*1000
local x = c
local now
repeat
for i = 1, c do
ms1()
end
now = time()
c = 1000*(s+st-now)
until (s+st) < now
return now - s, x
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment