Manually optimize the tail call away yourself and get a very nice improvement in speed. I switched back to your node's parent's code because it was recursive and yours wasn't. I haven't decided where exactly recursion is so utterly losing

Rate normal goto amp unrolled normal 7.91/s -- -75% -91% -95% goto 31.9/s 304% -- -63% -81% amp 85.5/s 981% 168% -- -50% unrolled 169/s 2044% 431% 98% --
use strict; use warnings; no warnings 'recursion'; use Benchmark 'cmpthese'; use List::Util 'shuffle'; my @work = shuffle( 1 .. 2000, ); my @result = @work; cmpthese( 100, { normal => sub { @result = rec_max_normal(@work) }, goto => sub { @result = rec_max_goto(@work) }, amp => sub { @result = rec_max_amp(@work) }, unrolled => sub { @result = rec_max_unrolled(@work) }, } ); sub rec_max_unrolled { { return () unless @_; my ( $h1, $h2 ) = ( shift, shift ); if (@_) { unshift @_, ( $h1 > $h2 ) ? $h1 : $h2; redo; } elsif ( defined $h2 ) { return ( $h1 > $h2 ) ? $h1 : $h2; } } } sub rec_max_normal { { return () unless @_; my ( $h1, $h2 ) = ( shift, shift ); if (@_) { unshift @_, ( $h1 > $h2 ) ? $h1 : $h2; rec_max_normal(@_); } elsif ( defined $h2 ) { return ( $h1 > $h2 ) ? $h1 : $h2; } } } sub rec_max_amp { { return () unless @_; my ( $h1, $h2 ) = ( shift, shift ); if (@_) { unshift @_, ( $h1 > $h2 ) ? $h1 : $h2; &rec_max_amp; } elsif ( defined $h2 ) { return ( $h1 > $h2 ) ? $h1 : $h2; } } } sub rec_max_goto { { return () unless @_; my ( $h1, $h2 ) = ( shift, shift ); if (@_) { unshift @_, ( $h1 > $h2 ) ? $h1 : $h2; goto &rec_max_goto; } elsif ( defined $h2 ) { return ( $h1 > $h2 ) ? $h1 : $h2; } } }

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊


In reply to Re^8: Behold! The power of recursion. by diotalevi
in thread Behold! The power of recursion. by DigitalKitty

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.