Skip to content

Instantly share code, notes, and snippets.

@radarek
Created January 3, 2009 18:24
Show Gist options
  • Save radarek/42915 to your computer and use it in GitHub Desktop.
Save radarek/42915 to your computer and use it in GitHub Desktop.
$ ~/opt/bin/erlc tak.erl
$ ~/opt/bin/erl -noshell -s tak bm -s init stop
1.277981
1.243637
1.241756
1.239656
1.239023
$ ~/opt/bin/erlc +native tak.erl
$ ~/opt/bin/erl -noshell -s tak bm -s init stop
0.219017
0.218914
0.218162
0.218196
0.218384
$ jruby --version
jruby 1.1.7 (ruby 1.8.6 patchlevel 114) (2009-01-03 rev ) [i386-java]
$ jruby --server tak.rb 10
3.245606
2.087804
2.131338
2.13407
2.15724
2.15794
2.156911
2.156623
2.156311
2.154496
$ ~/opt/bin/perl -v
This is perl, v5.10.0 built for x86_64-linux
$ ~/opt/bin/perl tak.pl
19.7152860164642
19.5772659778595
19.5895590782166
19.5708420276642
19.6651229858398
19.5898451805115
$ php --version
PHP 5.2.6-2ubuntu4 with Suhosin-Patch 0.9.6.2 (cli) (built: Oct 14 2008 20:18:13)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
with XCache v1.2.2, Copyright (c) 2005-2007, by mOo
$ php tak.php
17.4115741253
17.1261250973
17.1329028606
17.1747589111
17.1358361244
$ python --version
Python 2.5.2
$ python tak.py
6.84
6.48
6.44
6.49
6.64
$ ~/opt/bin/python --version
Python 2.6.1
$ ~/opt/bin/python tak.py
6.33
6.30
6.30
6.26
6.22
$ ~/opt/bin/python3.0 --version
Python 3.0
$ ~/opt/bin/python3.0 tak.py
7.89
7.78
7.77
7.78
7.78
$ ruby1.8.6 -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [x86_64-linux]
$ ruby1.8.6 tak.rb 5
16.057329
15.940528
15.928416
16.133884
15.992749
$ ruby1.9.1 -v
ruby 1.9.1 (2008-12-30 patchlevel-0 revision 21201) [x86_64-linux]
$ ruby1.9.1 tak.rb 5
2.22714482
2.107448652
2.125363523
2.12817825
2.118874232
-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)
)
.
<?
function tak($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));
}
}
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);
#!/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 ) );
}
}
import time
def tak(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))
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))
def tak 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))
end
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment