lua-users home
lua-l archive

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


Let's benchmark modulo operator.
The code is here:  pastebin.com/13XBxdn6
The Lua executables are x64 exe-files built with VS2010.
 
The results:
 
   C:\>lua51.exe modulo_benchmark.lua
   CPU seconds for floating point modulo:  2.377
 
   C:\>lua52.exe modulo_benchmark.lua
   CPU seconds for floating point modulo:  2.519
 
   C:\>lua53.exe modulo_benchmark.lua
   CPU seconds for integer modulo: 3.84
   CPU seconds for floating point modulo:  6.016
 
   C:\>lua54.exe modulo_benchmark.lua
   CPU seconds for integer modulo: 3.765
   CPU seconds for floating point modulo:  6.241
 
Modulo operator become slower starting with Lua 5.3.
This is because of Lua 5.2 formula
   a%b = a-floor(a/b)*b
was replaced with more slow, but more correct and more precise Lua 5.3 algorithm
   m=fmod(a,b);  if (m*b<0) m+=b;          return m;  // for floats
   m=a%b;        if (m!=0 && a^b<0) m+=b;  return m;  // for ints
 
BTW, changing (m!=0 && a^b<0) to (m!=0 && m^b<0) might make the code more optimizer-friendly:
lifetimes of local variables "a" and "m" are non-overlapping, hence both these variables could share single CPU register.
 
Ok, modulo operator has changed its implementation, that's why it become slower.
Let's benchmark another operator that didn't change its semantics since Lua 5.1.
For example, addition.
Use the same benchmarking code as previously, but replace all percents with pluses.
 
The results:
 
   C:\>lua51.exe addition_benchmark.lua
   CPU seconds for floating point addition:        1.185
 
   C:\>lua52.exe addition_benchmark.lua
   CPU seconds for floating point addition:        1.093
 
   C:\>lua53.exe addition_benchmark.lua
   CPU seconds for integer addition:       1.107
   CPU seconds for floating point addition:        1.556
 
   C:\>lua54.exe addition_benchmark.lua
   CPU seconds for integer addition:       1.452
   CPU seconds for floating point addition:        1.717
 
Why numeric addition (the simplest thing in Lua) become slower starting with Lua 5.3?
Is it because of detecting operand subtypes (int/float) inside every "ADD" instruction?
 

 On Mon, Jul 30, 2018 at 5:40 AM, 云风 Cloud Wu wrote:

Arithmetic computations written for Lua 5.1 appear to be 1.25 times slower in Lua 5.4 (compared to Lua 5.1)
(Probably, this happened because of heavy mixing of floats with integers in arithmetic expressions.)


Maybe one of reason is that `Integer modulus` is slower than `Float modulus`, See function `luaV_mod` in lvm.c .


The situation is strange:
As displayed above, on usual computer under x64 Windows integer operations in Lua are faster than floating point ones.
But on 10-year-old PC under x64 Linux integer operations are slower than floating point operations:
 
   $ lua -v modulo_benchmark.lua
   Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
   CPU seconds for integer modulo:    7.368922
   CPU seconds for floating point modulo:    5.265935
 
   $ lua -v addition_benchmark.lua
   Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
   CPU seconds for integer addition:    1.893307
   CPU seconds for floating point addition:    1.762248
 
So, this contradiction shows all these benchmarks are probably valueless :-)