Created
December 5, 2016 07:23
-
-
Save greatwolf/660e4dbd917dfa3116e6adc4b24c0488 to your computer and use it in GitHub Desktop.
This file contains 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 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