sub buk { use List::Util qw[sum]; my( $hashref, $start, $end ) = @_; # Added sanity check. Slowed it a tad return undef unless exists $hashref->{$start} and exists $hashref->{$end}; my @keys = grep{ $_ >= $start && $_ <= $end } keys %{ $hashref }; return sum( @{ $hashref }{ @keys } ) / @keys; } __END__ P:\test>278169 0.503353665487943 0.503353665487943 Rate orig buk orig 1.28/s -- -42% buk 2.23/s 73% -- #### sub buk2 { use List::Util qw[sum]; return undef unless exists $_[0]->{ $_[1] } and exists $_[0]->{ $_[2] }; return sum( @{ $_[0] }{ $_[1] .. $_[2] } ) / ( $_[2] - $_[1] + 1 ); } __END__ P:\test>278169 0.503353665487943 0.503353665487943 0.503353665487943 Rate orig buk buk2 orig 1.30/s -- -41% -81% buk 2.19/s 69% -- -67% buk2 6.70/s 417% 206% --