http://www.perlmonks.org?node_id=1032046


in reply to Counter - number of tags per interval

Hi

I do not know if this will help and also don't know if this will be faster, but I would try to use a different data structure: a hash of arrays with each array containing 3 elements: the interval lower limit, the interval upper limit and the tag count.

As a (very light) proof of concept, I will simplify the problem by simply forgetting about the a, b et so on, but just using a simple list of ranges. Or, if you prefer, I will deal only with your data related to letter a. Since I do not understand what your tags are about, I will just add 1 to the count when the value of the variable falls within the range.

This gives me the following session under the Perl debugger:

DB<49> @c = ([12,15], [12,17], [13,14], [14,19]) DB<50> x @c 0 ARRAY(0x20498b98) 0 12 1 15 1 ARRAY(0x20499588) 0 12 1 17 2 ARRAY(0x2049c428) 0 13 1 14 3 ARRAY(0x2049c488) 0 14 1 19 DB<51> $e = 17 DB<52> $_->[2]++ foreach grep {$e <= $_->[1]} grep {$e >= $_->[0]} @ +c DB<53> x @c 0 ARRAY(0x20498b98) 0 12 1 15 1 ARRAY(0x20499588) 0 12 1 17 2 1 2 ARRAY(0x2049c428) 0 13 1 14 3 ARRAY(0x2049c488) 0 14 1 19 2 1 DB<54> $e = 13; DB<55> $_->[2]++ foreach grep {$e <= $_->[1]} grep {$e >= $_->[0]} @ +c DB<56> x @c 0 ARRAY(0x20498b98) 0 12 1 15 2 1 1 ARRAY(0x20499588) 0 12 1 17 2 2 2 ARRAY(0x2049c428) 0 13 1 14 2 1 3 ARRAY(0x2049c488) 0 14 1 19 2 1 DB<57>