Fibonacci(50) performance : Java > C > C++ > D > Go > Terra (Lua) > Lua-JIT (Lua)

Recently,  I have been spending some time to learn D and Go  languages.  D Lang is an evolution of C++ where as Go is being claimed to be an evolution of C but I think  it is a Google's attempt to replace dependency on the Java.

I really like the simplicity of Go where the learning curve is very lean but accepting it as a system level programming would be a tough sell.   I think it is more for Java/Python developers for building the enterprise softwares rather than using it for hardware/device-drivers/embedded programming, but you never know as  Google is very successful selling  Android on low powered smart devices.

I did some performance bench for C, C++, D and Go  languages using the Fibonacci algorithm.

Results: (see updated results at the end)

For Fibonacci(25), C++ >= Go > C >= Lua-JIT > D > Lua-Terra > Java 1.6

For Fibonacci(50), Java > C > C++ > D-ldc > D-dmd > Go > Lua-Terra > Lua-JIT

Now surprisingly,  Java out performed C/C++  for Fibonacci (50) which hurts my ego :) !!

Language % C++ Speed Compiler/VM Flags
C++ 100.0000 Apple LLVM version 6.0 -O3
GO 100.0000 go version go1.3.3 darwin/amd64
C 77.7778 Apple LLVM version 6.0 -O3
LUA 77.7778 LuaJIT 2.0.3
D 63.6364 dmd  -m64 -O  -inline -noboundscheck
D 63.6364 ldc -m64 -O  -inline
LUA 43.7500 Terra
JAVA 1.6 43.4783 1.6.0_65-b14-462-11M4609)
JAVA 1.6 169.6710 1.6.0_65-b14-462-11M4609)
C 101.9846 Apple LLVM version 6.0 -O3
C++ 100.0000 Apple LLVM version 6.0 -O3
D 92.6376 ldc -m64 -O  -inline
D 81.7197 dmd  -m64 -O  -inline -noboundscheck
GO 76.7760 go version go1.3.3 darwin/amd64
LUA 43.9684 Terra
LUA 38.9649 LuaJIT 2.0.3

For Source code and results:  check out my github project.

It seems Clang on MacOS has some issue. I executed these on my Linux Virtual machine with gnu g++ and g++ is outperforming Java. My ego is intact :)

$g++ --version
g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4)
$g++ -O3 fib.cpp
time ./a.out 50
real 0m47.991s
user 0m47.981s
sys 0m0.000s

$java -version
java version "1.7.0_51"
$time java fib 50
real 0m51.897s
user 0m51.815s
sys 0m0.113s

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.


>time luajit-2.0.3 fib.lua 25

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

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

Running Terra test
real 0m0.016s
user 0m0.005s
sys 0m0.010s

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


>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.

Apple Push Notification Service (APNS) Simulator

APNS simulator implementes APNS Specs for a simple and an enhanced push notification.
Once you have downloaded/installed LuaJit and luarocks, other dependencies can be installed using luarocks
luarocks install copas

luarocks install LuaSec

luarocks install LuaLogging
Usage:   apns-sim.lua -t ssl_enabled [ -k ssl_key -c ssl_cert] [ -s server -p port -l loglevel -]

Here ssl_key  and ssl_cert fields are mandatory if ssl_enabled is set to true.  

ssl_enabled :  defaul false
server : default value is

port  :  default 8080

loglevel : default value is 'warn'

e.g. for ssl connection:
lua  apns-sim.lua -t true -k ./key.pem -c ./cert.pem

for non-ssl connection:  lua  apns-sim.lua 
When client connect to this simulator and send a push notification, you will see log entries on console.
Wed Oct 15 09:16:13 2014 INFO Received client connection  from '':
Wed Oct 15 09:16:13 2014 INFO Received notification: command=1; id=21; expiry=1413382573; token=adf3b210e7adf35f540f45b2697760d9d41081569dc4509ee98bb4d4c92a72ae; payload={"aps":{"alert":{"body":"Hello World"}}}