Depending on your application, you can trade memory for speed. So if all you are really interested in is the information you extract above, you can pre-calculate and store the results. Or even just cache results like this if you will call repeatably...
my %maxIntesityCache;
sub getIntensity {
my $m = shift;
# Do we know this already?
return $maxIntesityCache{$m}
if defined $maxIntesityCache{$m};
...as for your function, except you can probably change
the core to remove the exists test and/or one of the other
suggestions in this thread...
# Remember this for later
$maxIntensityCache{$m} = $intensity;
return $intensity;
}
Precalculation may give you a win because you can run through the keys once, keeping a window from -0.3 to +0.3 open on your key at all times.
Another thought...if all your numeric work is to precision '0.1' you might get a nice speedup by having your key as 'mass*10' so you are only storing integers. You can then:
- avoid the sprintf you are using to control precision issues, which must hurt you now I come to think of it
- you can switch perl into 'use integer' mode which might help too.
Good luck...