On Aug 26, 2018, at 8:51 AM, Egor Skriptunoff <email@example.com> wrote:
Depends on the application, there might not be a price.
My primepi.lua, which count numbers of primes <= n, uses lots of mod's
For prime.pi(10^12), it called mod 16,055,541 times.
On my old Pentium 3, LuaJIT 1.1.8 were using fmod to implenent %.
Just to be sure, I edit away the %, replacing with fmod:
luajit -O primepi.lua 1e12 => 37607912018, take 54.8 seconds
Recompile LuaJIT using Lua 5.1 definition of mod for math.fmod,
i.e. ((a) - floor((a)/(b))*(b)), it run slower (64.0 seconds) !?
My guess is if a and b are not extremely far off, fmod is faster,
because it avoided the expensive division, using subtractions instead.