Skip to content

Instantly share code, notes, and snippets.

@guilleiguaran
Created July 19, 2011 04:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilleiguaran/1091318 to your computer and use it in GitHub Desktop.
Save guilleiguaran/1091318 to your computer and use it in GitHub Desktop.
Languages benchmarks
#include <stdio.h>
int fib(int n) {
if (n == 0 || n == 1) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
}
int main(int argc, char *argv[]) {
int i = 0;
for(i = 0; i < 36; i++) {
printf("n=%d => %d\n", i, fib(i));
}
return 0;
}
class Fib {
static Int fib (Int n) {
if (n == 0 || n == 1) {
return n
} else {
return fib(n-1) + fib(n-2)
}
}
public static Void main () {
36.times |n| {
echo("n=$n => ${fib(n)}")
}
}
}
function fib(n) {
if(n == 0 || n == 1) {
return n;
} else {
return fib(n-1) + fib(n-2);
}
}
for(i = 0; i < 36; i++) {
console.log("n="+i+" => "+fib(i));
}
<?php
function fib($n) {
if ($n == 0 || $n == 1) {
return $n;
}
else {
return fib($n - 1) + fib($n - 2);
}
}
for ($i = 0; $i < 36; $i++) {
printf("n=%d => %d\n",$i, fib($i));
}
?>
def fib(n):
if n == 0 or n == 1:
return n
else:
return fib(n-1) + fib(n-2)
for i in range(36):
print("n="+str(i)+" => "+str(fib(i)))
def fib(n)
if n == 0 || n == 1
n
else
fib(n-1) + fib(n-2)
end
end
36.times do |i|
puts "n=#{i} => #{fib(i)}"
end
object Fibonacci {
def fib( n: Int): Int = {
if(n == 0 || n == 1) {
return n;
} else {
return fib(n-1) + fib(n-2);
}
}
def main(args: Array[String]): Unit = {
for (i <- 0 until 36) {
println("n="+i+" => "+fib(i))
}
}
}
public class Fibonacci {
public static int fib(int n) {
if(n == 0 || n == 1) {
return n;
} else {
return fib(n-1) + fib(n-2);
}
}
public static void main(String[] args) {
for(int i = 0; i < 36; i++) {
System.out.println("n="+i+" => "+fib(i));
}
}
}
System information:
Macbook Pro (13" 2011)
Intel Core i5-2415M @ 2.3 GHz
4GB 1333MHz DDR3
Mac OS X 10.6.8 (64 bits)
GCC 4.2.1:
./fib 0.09s user 0.00s system 98% cpu 0.089 total
LLVM 2.8:
./fib 0.10s user 0.00s system 98% cpu 0.103 total
Java 1.7:
java Fibonacci 0.44s user 0.04s system 132% cpu 0.361 total
Scala 2.9:
scala Fibonacci 0.90s user 0.08s system 137% cpu 0.708 total
Nodejs 0.4.8:
node fib.js 1.23s user 0.01s system 99% cpu 1.242 total
Rubinius 2.0:
ruby fib.rb 2.10s user 0.04s system 107% cpu 2.000 total
MacRuby 0.10:
macruby fib.rb 2.02s user 0.01s system 99% cpu 2.035 total
Fantom 1.0.58:
fan fib.fan 3.88s user 0.16s system 187% cpu 2.153 total
JRuby 1.7:
jruby fib.rb 3.18s user 0.09s system 117% cpu 2.796 total
Ruby 1.9.2:
ruby fib.rb 6.62s user 0.02s system 98% cpu 6.725 total
Jython 2.5.2:
jython fib.py 15.12s user 0.69s system 122% cpu 12.940 total
Python 2.7:
python2.7 fib.py 15.79s user 0.04s system 99% cpu 15.845 total
Python 3.2:
python3.2 fib.py 17.00s user 0.04s system 99% cpu 17.048 total
PHP 5.3.4:
php fib.php 20.57s user 0.06s system 99% cpu 20.660 total
Ruby Enterprise Edition 1.8.7:
ruby fib.rb 35.80s user 0.08s system 99% cpu 35.982 total
Ruby 1.8.7:
ruby fib.rb 42.37s user 0.09s system 99% cpu 42.600 total
@c4milo
Copy link

c4milo commented Jul 19, 2011

sería bacano que agregaras un benchmark con CLANG

@c4milo
Copy link

c4milo commented Jul 19, 2011

another question, your machine is 32 or 64 bits?

@guilleiguaran
Copy link
Author

guilleiguaran commented Jul 19, 2011 via email

@c4milo
Copy link

c4milo commented Jul 19, 2011

compilation flags will be cool too :)

@c4milo
Copy link

c4milo commented Jul 19, 2011

also in the java version, try using StringBuilder instead of concatenating the strings with
"+"

@guilleiguaran
Copy link
Author

Just re-run gcc and llvm using flags:

gcc -O3 -mmmx -msse4 -mtune=native -o fib fib.c
./fib 0.09s user 0.00s system 98% cpu 0.089 total

llvmc -O3 -o fib fib.c
./fib 0.10s user 0.00s system 98% cpu 0.103 total

@c4milo
Copy link

c4milo commented Jul 19, 2011

is there any special reason to use -mmmx and -msse4? do they make any difference in your benchmarks?

@guilleiguaran
Copy link
Author

no, looks like only -O3 do the hard work here :). Thanks!!!

@julianduque
Copy link

Great and simple benchmark guilleiguaran!

@c4milo
Copy link

c4milo commented Jul 19, 2011

@guilleiguaran actually the comment about the flags was thanks to one of my coworkers :) Andrew Turley

@guilleiguaran you might gain also some performance in the C version specifying "n" as a constant in the fib function.
int fib(const int n) (Andrew Turley's comment too)

the same in the nodejs version, make sure you are using nodejs with crankshaft enabled in V8. It should improve the performance a lot.

@c4milo
Copy link

c4milo commented Jul 19, 2011

@julianduque hey xphree, sh4dow here :)

@julianduque
Copy link

@c4milo hey sh4dow!! :) greetz!!

@nebiros
Copy link

nebiros commented Jul 23, 2011

go fantom go! ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment