Skip to content

Instantly share code, notes, and snippets.

@malkia

malkia/badif.lua

Created May 25, 2010
Embed
What would you like to do?
-- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment