lua-users home
lua-l archive

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


2018-08-09 16:38 GMT-03:00 Egor Skriptunoff <egor.skriptunoff@gmail.com>:
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 :-)

 
On a recent Linux 4.15.0-24-generic x86_64
gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)
Intel(R) Core(TM) i5-6500 CPU

MODULO

lua-5.2.4/src/lua teste_lua54_benchmark.lua

CPU seconds for integer modulo:    1.949047
CPU seconds for floating point modulo:    1.934812

lua-5.3.5/src/lua teste_lua54_benchmark.lua

CPU seconds for integer modulo:    3.228948
CPU seconds for floating point modulo:    1.986498

lua-5.4.0-work2/src/lua teste_lua54_benchmark.lua

CPU seconds for integer modulo:    3.201198
CPU seconds for floating point modulo:    2.24338

OBS: CPU throttling ON

ADDITION

lua-5.2.4/src/lua teste_lua54_benchmark_plus.lua

CPU seconds for integer addition:    0.881306
CPU seconds for floating point addition:    0.88283

lua-5.3.5/src/lua teste_lua54_benchmark_plus.lua

CPU seconds for integer addition:    0.850767
CPU seconds for floating point addition:    0.882885

lua-5.4.0-work2/src/lua teste_lua54_benchmark_plus.lua

CPU seconds for integer addition:    1.04777
CPU seconds for floating point addition:    1.071874

OBS: Lua 5.4 compiles (luac -l) with a lot more "LOADK"
instructions compared to lua 5.2 and 5.3.

Is "LOADK" primarily responsible for the performance gap
between versions 5.3 and 5.4?

--
Rodrigo Azevedo Moreira da Silva