in reply to calculate average from range of hash values (optimize)
If your lucky enough to have access to the XS version of List::Util this would probably run much faster still.
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% --
Second attempt (417%)
Once I noticed that your code assumes that a contiguous range of keys will exist between the start and end values, it can go much quicker.
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% --
|
|---|