lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Aug 26, 2018, at 8:51 AM, Egor Skriptunoff <egor.skriptunoff@gmail.com> wrote:

On Sun, Aug 26, 2018 at 2:05 PM, Dirk Laurie wrote:
the correct value ... that Lua 5.3 returns.

 
Indeed, fmod() is doing its job very well.
IMO, fmod() does worth its performance price.

Depends on the application, there might not be a price.

My primepi.lua, which count numbers of primes <= n, uses lots of mod's

https://github.com/achan001/PrimePi

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.