Fibonacci in different languages.

5 views
Skip to first unread message

Jörgen Lundberg

unread,
Dec 17, 2009, 4:29:49 AM12/17/09
to go...@googlegroups.com
Hi all and thanks for a great Got.rb last night.

I talked to some people about calculating fib(40) with different
laungages and beeing surprised that running the java version was
faster than the c-version. Calculating a Fibonacci number with
recursion probably allows the JVM to perform some heavy optimizations.

Anyway below is a log of running the different versions.

http://en.wikipedia.org/wiki/Fibonacci_number
http://stackoverflow.com/questions/105834/does-the-jvm-prevent-tail-call-optimizations


Cheers,

/Jörgen


Hardware:
Model Name: iMac
Processor Name: Intel Core i7
Processor Speed: 2,8 GHz
Number Of Processors: 1
Total Number Of Cores: 4
Memory: 8 GB

[Xenu:jorgen]~/Downloads/slask>l
total 88
-rw-------@ 1 jorgen staff 471 15 Sep 07:04 Fib.class
-rw-------@ 1 jorgen staff 264 13 Sep 08:46 Fib.java
-rwxr-xr-x@ 1 jorgen staff 12372 15 Sep 07:13 fib*
-rw-------@ 1 jorgen staff 195 13 Sep 09:39 fib.c
-rw-------@ 1 jorgen staff 107 13 Sep 09:34 fib.py
-rw-------@ 1 jorgen staff 87 12 Sep 21:40 fib.rb
-rw-------@ 1 jorgen staff 114 13 Sep 08:49 fib.scala

[Xenu:jorgen]~/Downloads/slask>cat Fib.java
public class Fib {
public static void main(String[] args) {
System.out.println(fib(40));
}

private static long fib(int i) {
if(i < 2) {
return i;
} else {
return fib(i - 2) + fib(i -1);
}
}
}

[Xenu:jorgen]~/Downloads/slask>cat fib.c
#include <stdio.h>

int main(int argc, char *argv[]) {
printf("%d\n", fib(40));
}

int fib(unsigned int a) {
if (a < 2) {
return a;
}
else {
return fib(a - 1) + fib(a - 2);
}
}

[Xenu:jorgen]~/Downloads/slask>cat fib.py
def fib(a):
if a < 2:
return a
else:
return fib(a - 1) + fib(a - 2)

print fib(40)

[Xenu:jorgen]~/Downloads/slask>cat fib.rb
def fib(a)
if a < 2
a
else
fib(a - 1) + fib(a - 2)
end
end

puts fib(40)

[Xenu:jorgen]~/Downloads/slask>cat fib.scala
def fib(a : Int): Int = {
if (a < 2)
a
else
fib(a - 1) + fib(a - 2)
}
println (fib(40))

[Xenu:jorgen]~/Downloads/slask>time java Fib
102334155

real 0m0.673s
user 0m0.686s
sys 0m0.035s
[Xenu:jorgen]~/Downloads/slask>time ./fib
102334155

real 0m0.690s
user 0m0.683s
sys 0m0.002s
[Xenu:jorgen]~/Downloads/slask>time scala fib.scala
102334155

real 0m1.345s
user 0m0.972s
sys 0m0.066s
[Xenu:jorgen]~/Downloads/slask>time python fib.py
102334155

real 0m55.822s
user 0m55.806s
sys 0m0.013s
[Xenu:jorgen]~/Downloads/slask>time ruby fib.rb
102334155

real 2m0.940s
user 2m0.517s
sys 0m0.422s

[Xenu:jorgen]~/Downloads/fib>time jruby fib.rb
102334155

real 0m19.790s
user 0m19.944s
sys 0m0.099s

[Xenu:jorgen]~/Downloads/slask>java -version
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03-219)
Java HotSpot(TM) 64-Bit Server VM (build 14.1-b02-90, mixed mode)

[Xenu:jorgen]~/Downloads/slask>scala -version
Scala code runner version 2.7.7.final -- Copyright 2002-2009, LAMP/EPFL

[Xenu:jorgen]~/Downloads/slask>python --version
Python 2.6.1

[Xenu:jorgen]~/Downloads/slask>ruby --version
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]

[Xenu:jorgen]~/Downloads/fib>jruby --version
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-12-17 6586) (Java
HotSpot(TM) 64-Bit Server VM 1.6.0_15) [x86_64-java]

fib.tar.gz

Jonas Nicklas

unread,
Dec 17, 2009, 7:10:52 AM12/17/09
to go...@googlegroups.com
Go MacRuby!!!

jonas@morbo ~/Desktop$ time ruby fib.rb
102334155

real 2m47.552s
user 2m46.849s
sys 0m0.456s

jonas@morbo ~/Desktop$ time macruby fib.rb
102334155

real 0m5.311s
user 0m5.239s
sys 0m0.022s

jonas@morbo ~/Desktop$ macrubyc fib.rb -o fib
jonas@morbo ~/Desktop$ time ./fib
102334155

real 0m3.800s
user 0m3.762s
sys 0m0.033s

jonas@morbo ~/Desktop$ gcc fib.c -o fibc
jonas@morbo ~/Desktop$ time ./fibc
102334155

real 0m2.866s
user 0m2.835s
sys 0m0.008s


Yes, MacRuby is 44 times faster than Ruby 1.8. Even the non-compiled
version is less than 2x slower than fucking *C*. The AOT compiled
binary is only 30% slower! Take that Python!

/Jonas

> --
>
> You received this message because you are subscribed to the Google Groups "Got.rb" group.
> To post to this group, send email to go...@googlegroups.com.
> To unsubscribe from this group, send email to gotrb+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/gotrb?hl=en.
>
>
>

Jonas Nicklas

unread,
Dec 17, 2009, 7:14:12 AM12/17/09
to go...@googlegroups.com
Also: MacRuby is faster than Scala :D

jonas@morbo ~/Desktop$ time scala fib.scala
102334155

real 0m5.836s
user 0m1.701s
sys 0m0.121s


jonas@morbo ~/Desktop$ time macruby fib.rb
102334155

real 0m5.386s
user 0m5.288s
sys 0m0.044s

Reply all
Reply to author
Forward
0 new messages