Skip to content

Instantly share code, notes, and snippets.

@greatwolf
Created December 5, 2016 07:23
Show Gist options
  • Save greatwolf/660e4dbd917dfa3116e6adc4b24c0488 to your computer and use it in GitHub Desktop.
Save greatwolf/660e4dbd917dfa3116e6adc4b24c0488 to your computer and use it in GitHub Desktop.
local seq = require 'pl.seq'
local function bench (n, func, ...)
assert (n > 0)
assert (type(func) == 'function')
local start = os.clock ()
if n % 2 == 1 then
func (...)
n = n - 1
end
while n > 0 do
func (...)
func (...)
n = n - 2
end
return os.clock () - start
end
local function get_random(n, l, u)
local r = seq.random (n,l,u)
repeat until not r()
end
local function get_random2(n, l, u)
local r = seq.random2 (n,l,u)
repeat until not r()
end
local runtime
local function bench_parm (n, fn, nrand)
runtime = bench (n or 100000, fn, nrand, 1, 1000)
print ( ("elapse time: %.3f secs"):format (runtime) )
end
bench_parm (5000, get_random, 100)
bench_parm (5000, get_random, 1000)
bench_parm (5000, get_random, 10000)
print( ("-"):rep(80) )
bench_parm (5000, get_random2, 100)
bench_parm (5000, get_random2, 1000)
bench_parm (5000, get_random2, 10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment