in reply to Perl hit counter for multiple ranges, optimization or options in other languages?
You could go faster by completing your rangeHash straightaway without going through dataHash first.
Assuming your ranges can't overlap, all you have to do is compute the range from the value and use it as a key. For example: (the getRange sub is far from being perfect, I went for a simple solution since you probably already have code to check which range you are in) :
The result is :use Data::Dumper; sub getRange { my $value = shift; warn "Unexpected value $value," and return if $value < 1; return '1-19' if $value < 20; return '20-29' if $value < 30; return '30-39' if $value < 40; return '40-59' if $value < 60; warn "Out of range value $value," and return; } my %hits; while (<DATA>) { my $range = getRange($_); $hits{$range}++ if defined $range; } print Dumper \%hits; __DATA__ 12 14 15 20 21 50 42
$VAR1 = { '1-19' => 3, '40-59' => 2, '20-29' => 2 };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl hit counter for multiple ranges, optimization or options in other languages?
by SaktiMonk (Initiate) on Aug 07, 2013 at 22:42 UTC |