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

11 comments:

Anonymous said...

You could try freepascal:

program Project1;
uses sysutils;
function fib(n:int64):int64 ;
begin
if n < 2 then fib:=n
else
fib:=(fib(n-1)+fib(n-2)) ;
end;
begin
writeln('Language Pascal:',(fib(strtoint(ParamStr(1)))));
end.

Anonymous said...

Why not using Java 8 or even Java 7? Java 6 reached end-of-life already.

Rohit Joshi said...

I did run with Java 8. Below are results. Java 8 seem to be slower than Java 6 in my results.

bash-3.2$ time /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/java -server fib 50
LANGUAGE Java 12586269025

real 0m53.373s
user 0m53.313s
sys 0m0.050s

Anonymous said...

Your benchmark is irrelevant.

Anonymous said...

As is that comment.

Anonymous said...

Please do "some performance bench".
Please don't just do a 10 line Fib!

At least take tasks/programs from the benchmarks game and do repeated measurements with different workloads.

Better, improve on what the benchmarks game already does.

Anonymous said...

Cool, fancy writing a proper benchmark now?

Unknown said...

My results contradict yours.

$ ./run.sh 50
Running java test
LANGUAGE JAVA: 12586269025

real 1m6.111s
user 1m5.957s
sys 0m0.048s


Running C++ test
LANGUAGE CPP:12586269025

real 0m59.461s
user 0m59.329s
sys 0m0.021s


Running C test
LANGUAGE C: 12586269025

real 0m59.342s
user 0m59.211s
sys 0m0.014s


$ gcc --version
gcc (Gentoo 4.7.3-r1 p1.4, pie-0.5.5) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
...
$ g++ --version
g++ (Gentoo 4.7.3-r1 p1.4, pie-0.5.5) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
...
$ java -version
java version "1.6.0_31"
OpenJDK Runtime Environment (IcedTea6 1.13.3) (Gentoo build 1.6.0_31-b31)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)

Rohit Joshi said...

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

I will update the blog. Thanks.

Rachel Lancaster said...

Nice Article!_

Jean J. Robertson said...

Nice article! You did work hard on your blog and I really appreciate your efforts. "Fibonacci" remind me of my Java Practical lab. Thanks for sharing this information and I think that Java is a way more good than other language as far as Performance and Security is concerned.