Skip to content

Instantly share code, notes, and snippets.

@hipertracker
Created January 26, 2009 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hipertracker/52854 to your computer and use it in GitHub Desktop.
Save hipertracker/52854 to your computer and use it in GitHub Desktop.
benchmark - Fibonacci sequence
MacBook Pro, Core Duo 2, 2.8GHz
Mac OS-X 10.6.5
$ cat fib.rb
def fib(n)
return n if n <= 1
fib(n - 1) + fib(n - 2)
end
require "benchmark"
Benchmark.bm do |make|
5.times do
make.report { fib(38) }
end
end
$ cat fib.py
from timeit import time
def fib(n):
if n <= 1: return n
return fib(n - 1) + fib(n - 2)
for i in range(5):
t = time.time()
fib(38)
print(time.time()-t)
$ cat fib.php
<?php
function fib($n) {
if ($n <= 1) return $n;
return fib($n - 1) + fib($n - 2);
}
for($i=0; $i<5; $i++) {
$t = time();
fib(38);
print(time()-$t)."\n";
}
?>
RESULTS (taken the fastest result)
MacRuby 0.7.1 (ruby 1.9.2) [universal-darwin10.0, x86_64]
= 2.71 s.
Rubinius 1.1.1 (1.8.7 dc4b4bf0 2010-11-16 JI) [x86_64-apple-darwin10.5.0]
= 3.39 s.
JRuby 1.5.6 (ruby 1.8.7 patchlevel 249) (2010-12-03 9cf97c3) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [x86_64-java]
= 3.91 s. (with -J-Djruby.compile.fastest=true -J-D-jruby.compile.fastops=true --server -J-D-jruby.compile.positionless=true)
= 7.65 s.
JRuby 1.6.0.dev (ruby 1.8.7 patchlevel 249) (2010-12-09 1a0964f) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_22) [darwin-x86_64-java]
= 5.28 (with --server)
= 5.56
Ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.5.0]
= 9.40 s.
Python 2.7.1 (r271:86832, Dec 3 2010, 20:18:48) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin
= 25.78 s.
PHP 5.3.3 (cli) (built: Oct 28 2010 10:24:50) Zend Engine v2.3.0
= 27 s.
Python 3.1.3 (r313:86834, Dec 9 2010, 02:17:59) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin
= 30.37 s.
Ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-darwin10.4.0], MBARI 0x6770, Ruby Enterprise Edition 2010.02
= 57.38 s.
PyPy 1.4 PyPy 1.4 (Python 2.5.2) (79541, Nov 26 2010, 10:18:11)
= 64.83 s.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment