Naive benchmark comparison of Lua and native C code
← Home...and why you shouldn't forget about FFI when developing games with LÖVE2D
$ cat fib.c
#include <stdio.h>
int fib(int n)
{
if(n < 2)
return n;
return fib(n - 1) + fib(n - 2);
}
int main(int argc, char *argv[])
{
printf("%d\n", fib(35));
return 0;
}
$ cat fib.lua
function fib(n)
if n < 2 then
return n
end
return fib(n - 1) + fib(n - 2)
end
print(fib(35))
$ gcc ./fib.c -o fib -march=native -O3
$ hyperfine -w 5 -r 10 'lua5.4 ./fib.lua' 'luajit -O3 ./fib.lua' './fib'
Benchmark 1: lua5.4 ./fib.lua
Time (mean ± σ): 1.169 s ± 0.033 s [User: 1.166 s, System: 0.002 s]
Range (min … max): 1.128 s … 1.236 s 10 runs
Benchmark 2: luajit -O3 ./fib.lua
Time (mean ± σ): 185.6 ms ± 5.8 ms [User: 182.5 ms, System: 2.6 ms]
Range (min … max): 177.2 ms … 193.9 ms 10 runs
Benchmark 3: ./fib
Time (mean ± σ): 26.3 ms ± 0.4 ms [User: 24.6 ms, System: 1.6 ms]
Range (min … max): 25.7 ms … 27.1 ms 10 runs
Summary
./fib ran
7.06 ± 0.24 times faster than luajit -O3 ./fib.lua
44.44 ± 1.42 times faster than lua5.4 ./fib.lua
You can compare different implementations of more complex algorithms in different programming languages here
Last update: