…this is not very efficient in Perl…
You may refer to some disappointing benchmark as this:
Rate goto_recur recursive tail_recur looping goto_recur 3437/s -- -59% -60% -90% recursive 8472/s 147% -- -1% -76% tail_recur 8569/s 149% 1% -- -76% looping 35417/s 931% 318% 313% --

where the benchmark is that of swampyankee above, adding ikegami's tail_recur and a derived version of that using your goto recipe:

sub _goto_recur { my ($x, $y) = @_; if ($x <= 1) { return $y; } else { @_ = ($x - 1, $x * $y); goto &_goto_recur; } } sub goto_recur { my ($x) = @_; return _goto_recur($x, 1); }
Then I was surprised that throwing out all lexicals and directly messing with @_ yields more pleasing results:
sub _goto_recur { if ($_[0] <= 1) { return $_[1]; } else { $_[1] *= $_[0]; --$_[0]; goto &_goto_recur; } } sub goto_recur { my ($x, $y) = (shift, 1); # We can't plug the constant 1 directly into $_[1] # since we want to change that (alias!) return _goto_recur($x, $y); }
Gives on my machine:
Rate recursive tail_recur goto_recur looping recursive 8456/s -- -1% -9% -77% tail_recur 8519/s 1% -- -8% -76% goto_recur 9278/s 10% 9% -- -74% looping 36011/s 326% 323% 288% --
Of course that code does not look very desirable from a maintainers point of view.

In reply to Re^2: Misunderstanding Recursion by pKai
in thread Misunderstanding Recursion by Andrew_Levenson

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.