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}

In reply to Re^5: Are monks hibernating? by shmem
in thread Are monks hibernating? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.