Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^5: Are monks hibernating?

by shmem (Chancellor)
on Feb 15, 2007 at 15:27 UTC ( #600217=note: print w/replies, xml ) Need Help??


in reply to Re^4: Are monks hibernating?
in thread Are monks hibernating?

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}

Replies are listed 'Best First'.
Re^6: Are monks hibernating?
by BrowserUk (Pope) on Feb 17, 2007 at 01:28 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://600217]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2020-10-29 16:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (274 votes). Check out past polls.

    Notices?