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