Skip to content

Instantly share code, notes, and snippets.

@secretfader
Created July 22, 2011 04:06
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 secretfader/1098860 to your computer and use it in GitHub Desktop.
Save secretfader/1098860 to your computer and use it in GitHub Desktop.
Python / MRI / REE / Rubinius / JRuby / PHP / Node.js Fib Shootout
fib = (n) ->
if n < 2
return n
else
return fib(n - 1) + fib(n - 2)
i = 0
while i <= 35
console.log 'n = ' + i + ' => ' + fib(i)
i++
function fib(n) {
if (n == 0 || n == 1) {
return n;
} else {
return (fib(n - 1) + fib(n - 2));
}
}
for(i = 0; i <= 35; 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++) {
echo "n = " . $i . " => " . fib($i) . "\n";
}
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 = %d => %d" % (i, 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
function fib(n) {
if (n < 2) {
return n;
} else {
return (fib(n - 1) + fib(n - 2));
}
}
for(i = 0; i <= 35; i++) {
console.log('n = ' + i + ' => ' + fib(i));
}
<?php
function fib($n) {
if ($n < 2) {
return $n;
} else {
return fib($n - 1) + fib($n - 2);
}
}
for ($i = 0; $i<36; $i++) {
echo "n = " . $i . " => " . fib($i) . "\n";
}
def fib(n):
if n < 2:
return n
else:
return fib(n - 1) + fib(n - 2)
for i in range(36):
print "n = %d => %d" % (i, fib(i))
def fib(n)
if n < 2
n
else
fib(n - 1) + fib(n - 2)
end
end
36.times do |i|
puts "n = #{i} => #{fib(i)}"
end
## Python 2.7.2
real 0m20.607s
user 0m19.925s
sys 0m0.078s
## Python 2.7.2 (version 2)
real 0m19.227s
user 0m18.929s
sys 0m0.032s
## Ruby Enterprise Edition, 1.8.7-2011.03
real 0m53.581s
user 0m40.564s
sys 0m0.067s
## Ruby Enterprise Edition, 1.8.7-2011.03 (version 2)
real 0m33.192s
user 0m32.848s
sys 0m0.040s
## PHP 5.3.6
real 0m24.491s
user 0m24.464s
sys 0m0.016s
## PHP 5.3.6 (version 2)
real 0m22.568s
user 0m21.094s
sys 0m0.040s
## Ruby 1.9.2-p290
real 0m12.324s
user 0m12.302s
sys 0m0.012s
## Ruby 1.9.2-p290 (version 2)
real 0m10.543s
user 0m10.346s
sys 0m0.020s
## JRuby 1.6.3
real 0m6.885s
user 0m0.006s
sys 0m0.029s
## JRuby 1.6.3 (version 2)
real 0m3.335s
user 0m0.006s
sys 0m0.027s
## Rubinius 2.0.0pre
real 0m2.754s
user 0m2.718s
sys 0m0.044s
## Rubinius 2.0.0pre (version 2)
real 0m2.414s
user 0m2.488s
sys 0m0.049s
## Node.js 0.4.9
real 0m2.240s
user 0m1.550s
sys 0m0.015s
## Node.js 0.4.9 (version 2)
real 0m1.552s
user 0m1.521s
sys 0m0.015s
## Coffee Script (version 2)
real 0m1.765s
user 0m1.604s
sys 0m0.028s
## It's interesting to see how Rubinius smashes every other Ruby implementation,
## and totally massacres Python 2.7.2. Everyone thinks Node.js is so fast
## (and maybe it is) but Rubinius can really put it to the test. I, for one,
## would much rather write Ruby than JavaScript for most of my application logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment