Created
December 9, 2010 03:20
-
-
Save hipertracker/734287 to your computer and use it in GitHub Desktop.
Benchmark (Tak) Erlang vs Ruby vs Python vs Perl vs PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Erlang vs Ruby vs Python vs Perl vs PHP | |
Erlang R14B (erts-5.8.1) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false] | |
= 0.16 s. | |
MacRuby 0.7.1 (ruby 1.9.2) [universal-darwin10.0, x86_64] | |
= 0.40 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] | |
= 1.76 s | |
Ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.5.0] | |
= 2.05 s. | |
Python 2.7.1 (r271:86832, Dec 3 2010, 20:18:48) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin | |
= 5.24 s. | |
Python 3.1.3 (r313:86834, Dec 9 2010, 02:17:59) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin | |
= 5.88 s. | |
PHP 5.3.3 (cli) (built: Oct 28 2010 10:24:50) Zend Engine v2.3.0 | |
= 8.00 s. | |
Perl 5.10.0 built for darwin-thread-multi-2level | |
= 20.56 s. | |
PyPy 1.4 (Python 2.5.2) (79541, Nov 26 2010, 10:18:11) | |
= 12.51 s. | |
SOURCE CODE | |
ERLANG: | |
-module(tak). | |
-export([tak/3, bm/0, bm_testcase/0]). | |
tak(X, Y, Z) -> | |
if | |
Y >= X -> Z; | |
true -> | |
tak( | |
tak(X - 1, Y, Z), | |
tak(Y - 1, Z, X), | |
tak(Z - 1, X, Y) | |
) | |
end | |
. | |
bm() -> | |
lists:foreach( | |
fun(_) -> | |
{Time, _} = timer:tc(tak, bm_testcase, []), | |
TimeSec = Time / 1000000, | |
io:format("~p~n", [TimeSec]) | |
end, | |
lists:seq(1, 5) | |
) | |
. | |
bm_testcase() -> | |
lists:foreach( | |
fun(_) -> | |
tak:tak(24, 16, 8) | |
end, | |
lists:seq(1, 10) | |
) | |
. | |
PHP: | |
<?php | |
function tak($x, $y, $z) { | |
if ($y >= $x) return $z; | |
return tak( tak($x-1, $y, $z), tak($y-1, $z, $x), tak($z-1, $x, $y)); | |
} | |
function bm_tak($n) { | |
for ($i = 0; $i < $n; $i++) { | |
$t1 = microtime(true); | |
$j = 0; | |
while ($j < 10) { | |
tak(24, 16, 8); | |
$j++; | |
} | |
$t2 = microtime(true) - $t1; | |
print("$t2\n"); | |
} | |
} | |
bm_tak(5); | |
?> | |
PERL: | |
#!/usr/bin/perl -w | |
use strict; | |
use Time::HiRes qw(time); | |
$|++; | |
bm_tak( 5 ); | |
sub bm_tak { | |
my $n = shift; | |
for( 0..$n ) { | |
my( $i, $x ) = ( 0, time ); | |
while( $i < 10 ) { | |
tak( 24, 16, 8 ); | |
$i++; | |
} | |
print time-$x . "\n"; | |
} | |
} | |
sub tak { | |
my( $x, $y, $z ) = @_; | |
if( $y >= $x ) { | |
return $z; | |
}else{ | |
return tak( tak( $x-1, $y, $z ), | |
tak( $y-1, $z, $x ), | |
tak( $z-1, $x, $y ) ); | |
} | |
} | |
PYTHON: | |
import time | |
def tak(x, y, z): | |
if y >= x: return z | |
return tak( tak(x-1, y, z), tak(y-1, z, x), tak(z-1, x, y)) | |
for i in range(0, 5): | |
t = time.time() | |
i = 0 | |
while i<10: | |
tak(24, 16, 8) | |
i+=1 | |
print("%.02f" % (time.time() - t)) | |
RUBY: | |
def tak x, y, z | |
return z if y >= x | |
tak( tak(x-1, y, z), tak(y-1, z, x), tak(z-1, x, y)) | |
end | |
N = (ARGV.shift || 1).to_i | |
N.times do | |
i = 0 | |
t = Time.now | |
while i<10 | |
tak(24, 16, 8) | |
i+=1 | |
end | |
puts(Time.now - t) | |
end |
Update:
- JRuby 9.0.5.0 = 3.7 s
- PyPy 5.0.0 = 3.8 s
- Ruby 2.3.0 = 4.7 s
- Ruby Topaz = 5.3 s
- PHP 7.0.5 = 10.1 s
- Python 2.7.11 = 13.6 s
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dodane