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: