Take a look at Perl's lousy showing in the recursive Benchmark for what I mean about subroutine/method performance.
Well, lousy verbose coding is a big issue here, too. Removing unnecessary allocation of variables and explicit return calls where possible, and replacing the named subs with coderefs
# The Computer Language Shootout
# http://shootout.alioth.debian.org/
# recursive test, by Andreas Koenig, Sep 24 2006
### Uses temp variables to help perl free memory earlier
use strict;
my($Ack,$Fib,$Tak);
$Ack = sub
{
my ($x, $y) = @_;
return $y + 1 if $x == 0;
return $Ack->($x - 1, 1) if $y == 0;
$Ack->($x - 1, $Ack->($x, $y - 1));
};
$Fib = sub
{
my ($n) = @_;
return 1 if $n < 2;
$Fib->($n - 2) + $Fib->($n - 1);
};
$Tak = sub
{
my ($x, $y, $z) = @_;
if ($y < $x) {
my $z1 = $Tak->($x - 1.0, $y, $z);
my $z2 = $Tak->($y - 1.0, $z, $x);
my $z3 = $Tak->($z - 1.0, $x, $y);
return $Tak->($z1, $z2, $z3);
} else {
return $z;
}
};
my $n = ($ARGV[0] || 0) - 1;
printf "Ack(%d,%d): %d\n",
3, $n + 1, $Ack->(3, $n + 1);
printf "Fib(%.1f): %.1f\n",
28.0 + $n, $Fib->(28.0 + $n);
printf "Tak(%d,%d,%d): %d\n",
$n * 3, $n * 2, $n, $Tak->($n * 3, $n * 2, $n);
printf "Fib(%d): %d\n",
3, $Fib->(3);
printf "Tak(%.1f,%.1f,%.1f): %.1f\n",
3.0,2.0,1.0, $Tak->(3.0,2.0,1.0);
gives a speed gain of roughly 25% - that's still slower than python, though...
update: fixed wording
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}