public
Last active

  • Download Gist
badif.lua
Lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# run with luajit (www.luajit.org)
# LuaJIT 2.0.0-beta4 commit 926f688cd0cc177a779ee3bdb2c6a346383dd8e4 Thu May 20 00:40:51 2010 +0200
#
# Results on my Windows Vista Professional x64 machine: http://browse.geekbench.ca/geekbench2/view/229361
# ../luajit/src/luajit badif.lua
# (i & 0x80000000) == 0 time: 1.043
# (i & 0xFFFFFFFF) == 0 time: 0.699
# (i & 0x00000001) == 0 time: 3.684
# (i & 0x00000003) == 0 time: 5.337
# (i & 0x00000002) == 0 time: 3.684
# (i & 0x00000004) == 0 time: 3.683
# (i & 0x00000008) == 0 time: 3.672
# (i & 0x00000010) == 0 time: 3.847
 
local band = bit.band
local tests = {
{ ["0x80000000"] = function() local s=0 for i=0,1073741824 do if 0==band(i, 0x80000000) then s=s+1 end end return s end },
{ ["0xFFFFFFFF"] = function() local s=0 for i=0,1073741824 do if 0==band(i, 0xFFFFFFFF) then s=s+1 end end return s end },
{ ["0x00000001"] = function() local s=0 for i=0,1073741824 do if 0==band(i, 0x00000001) then s=s+1 end end return s end },
{ ["0x00000003"] = function() local s=0 for i=0,1073741824 do if 0==band(i, 0x00000003) then s=s+1 end end return s end },
{ ["0x00000002"] = function() local s=0 for i=0,1073741824 do if 0==band(i, 0x00000002) then s=s+1 end end return s end },
{ ["0x00000004"] = function() local s=0 for i=0,1073741824 do if 0==band(i, 0x00000004) then s=s+1 end end return s end },
{ ["0x00000008"] = function() local s=0 for i=0,1073741824 do if 0==band(i, 0x00000008) then s=s+1 end end return s end },
{ ["0x00000010"] = function() local s=0 for i=0,1073741824 do if 0==band(i, 0x00000010) then s=s+1 end end return s end },
}
 
local function timeit(f)
local start = os.clock()
f()
return os.clock() - start
end
 
for k,v in pairs(tests) do
for k,v in pairs(v) do
print( "(i & " .. k .. ") == 0 time: " .. tostring(timeit(v)))
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.