Skip to content

Instantly share code, notes, and snippets.

@dpino
Created January 4, 2019 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpino/925a79494d3da27e3681e8b3acf67b32 to your computer and use it in GitHub Desktop.
Save dpino/925a79494d3da27e3681e8b3acf67b32 to your computer and use it in GitHub Desktop.
Microbenchmark for LuaJIT's modulo operations
local C = ffi.C
function fold(fn, times)
local now = os.clock()
for i=1,times do
fn()
end
return os.clock() - now
end
print("math.fmod: "..fold(function() return math.fmod(255, 8) end, 10000000))
print("%: "..fold(function() return 255 % 8 end, 10000000))
print("&: "..fold(function() return bit.band(255, 7) end, 10000000))
@dpino
Copy link
Author

dpino commented Jan 4, 2019

Example of execution:

$ luajit modulo.lua
math.fmod: 0.243867
%: 0.003274
&: 0.003262

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment