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

2 min read Original article ↗

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
FIBONACCI-25
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)
FIBONACCI-50
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.

Update:
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"
$javac fib.java
$time java fib 50
real 0m51.897s
user 0m51.815s
sys 0m0.113s