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; } } }