Fibonacci numbers: LuaJIT vs Terra

Recently I came across Terra Languange which is  a new low-level system programming language that is designed to interoperate seamlessly with the Lua programming language.

I thought of comparing the performance between Lua-Jit and Terra.  We all know Lua-JIT is extremely fast thanks to Mike Pall.

You can find fib.lua source code here as lang-compare  on my github.

FIBONACCI - 25

>time luajit-2.0.3 fib.lua 25

Running LUA-JIT 2.0.3 test
LANGUAGE LUA: 75025
real 0m0.009s
user 0m0.002s
sys 0m0.006s

Running fib.lua with Terra :
>time terra  fib.lua 25

Running Terra test
LANGUAGE LUA: 75025
real 0m0.016s
user 0m0.005s
sys 0m0.010s

Here LuaJIT is 77% faster than Terra for the same fib.lua file.

FIBONACCI - 50

>time luajit-2.0.3 fib.lua 50
Running LUA-JIT test
LANGUAGE LUA: 12586269025
real 3m8.326s
user 3m8.157s
sys 0m0.045s

>time terra  fib.lua 50
Running Terra test
LANGUAGE LUA: 12586269025
real 2m46.895s
user 2m46.734s
sys 0m0.043s

Here LuaJit is 56% slower than terra.   I ran the same tests multiple times and the results are consistently same where terra is out performing luajit by ~45% when ran for longer duration.



2 comments:

Anonymous said...

0m0.009s is too short execution time to be trusted as a benchmark. My guess is that Terra has some initial overhead that makes it appear slower only because the test run is so extremely short.

Unknown said...

This makes no sense. You ran Lua code in terra, not terra code. Terra uses LuaJIT internally to execute Lua code, but Terra is not a way to execute Lua faster - it's that you can use Lua code to generate low-level terra code. Terra benchmark could look like this:

terra fib(n: uint64): uint64
if n<2 then
return n
else
return fib(n-1) + fib(n-2)
end
end

local function main()
local n = tonumber(arg[1])
io.stdout:write("LANGUAGE TERRA: " .. tostring(fib(n)) .. "\n")
end
main()