use warnings; use strict; use Benchmark qw/cmpthese/; use List::Util::XS; use List::Util qw/sum/; my @array = 1.. 10000; my $expect = sum(@array); { use feature 'current_sub'; no warnings 'recursion'; sub SumArryRcs { 1==@_?$_[0]:1>@_?die:shift(@_)+__SUB__->(@_); } sub SumArryRcs2 { @_ ? shift(@_) + __SUB__->(@_) : 0; } } { my $agg = 0; sub SumArryRcs3 { if (@_) { $agg += pop; goto &SumArryRcs3 } else { my $res = $agg; $agg = 0; # reset return $res; } } } sub SumArrySplit { if (@_ >2) { return SumArrySplit( @_[ 0 .. $#_/2 ] ) +SumArrySplit(@_[ $#_/2+1 .. $#_ ] ); } elsif (@_ == 2) { return $_[0]+$_[1] } else { return $_[0]; } } # warn SumArryRcs3(@array); # warn SumArrySplit(@array); warn "Perl Version: $]\n"; warn "Array Length: ", scalar @array,"\n"; warn "Expected result: $expect\n"; #__END__ cmpthese(-1, { iterative => sub { my $agg = 0; $agg += $_ for @array; $agg == $expect or die; }, recursive => sub { SumArryRcs(@array) == $expect or die }, # recursive2 => # sub { SumArryRcs2(@array) == $expect or die }, recursive3 => sub { SumArryRcs3(@array) == $expect or die }, rec_split => sub { SumArrySplit(@array) == $expect or die }, eval => sub { eval(join "+", @array) == $expect or die }, listutil => sub { sum(@array) == $expect or die }, } );