in reply to Re: calculate average from range of hash values (optimize)
in thread calculate average from range of hash values (optimize)
Finally, that we're doing math on the values without checking their definedness suggests that valid values are never undef, in which case you can skip the double lookup.use List::Util qw(sum); sub average { my ($data, $first, $last) = @_; my @goodkeys = grep exists $data->{$_}, $first .. $last; sum( @{$data}{@goodkeys} ) / @goodkeys; }
That should be about as fast as you can get in Perl.use List::Util qw(sum); sub average { my ($data, $first, $last) = @_; my @values = grep defined, @{$data}{$first .. $last}; sum(@values) / @values; }
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re^2: calculate average from range of hash values (optimize)
by demerphq (Chancellor) on Jul 27, 2003 at 13:59 UTC | |
by Aristotle (Chancellor) on Jul 27, 2003 at 18:32 UTC |