Created
February 28, 2017 16:57
-
-
Save meepen/d694511399dd3948dae6f6b9fd0eb36a to your computer and use it in GitHub Desktop.
if bst > fn lookup tbl
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 clock = os.goodclock or os.clock | |
local fns = { | |
function(a,b) return a + b end, | |
function(a,b) return a - b end, | |
function(a,b) return a * b end, | |
function(a,b) return a / b end, | |
function(a,b) return a ^ b end, | |
function(a,b) return b + a end, | |
function(a,b) return b - a end, | |
function(a,b) return b * a end, | |
function(a,b) return b / a end, | |
function(a,b) return b ^ a end, | |
} | |
(function() | |
local cycles = 1e8 | |
local stime = clock() | |
local ops = #fns | |
math.randomseed(0) | |
for i = 1, cycles do | |
local n = math.random(1,ops) | |
local a = math.random() * 100 | |
local b = math.random() * 100 | |
local ret | |
if (n > 5) then | |
if (n > 7) then | |
if (n == 10) then | |
ret = b ^ a | |
elseif (n == 9) then | |
ret = b / a | |
else -- if (n == 8) then | |
ret = b * a | |
end | |
elseif (n == 7) then | |
ret = b - a | |
else -- if (n == 6) then | |
ret = b + a | |
end | |
elseif (n > 2) then | |
if (n == 5) then | |
ret = a ^ b | |
elseif (n == 4) then | |
ret = a / b | |
else -- if (n == 3) then | |
ret = a * b | |
end | |
elseif (n == 2) then | |
ret = a - b | |
else -- if (n == 1) then | |
ret = a + b | |
end | |
end | |
local etime = clock() | |
print("if bst", etime - stime) | |
stime = clock() | |
local fns = fns | |
math.randomseed(0) | |
for i = 1, cycles do | |
local n = math.random(1,ops) | |
local a = math.random() * 100 | |
local b = math.random() * 100 | |
local ret = fns[n](a, b) | |
end | |
etime = clock() | |
print("fn", etime - stime) | |
end)() |
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
$ ./tests/if-vs-fn.lua | |
if bst 3.821662902832 | |
fn 4.4534289836884 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment