You can reduce the number of comparisons by recognising that your ranges are a cover of the total range, and disjoint. That lets you check against just one of the bounds of each subrange. For example, in japhy's solution, the loop over @settings can be rewritten,
The savings are not always as great as they appear. A similar effect can be obtained by testing the upper bound first in the redundant form. The short-circuiting and operator will take care of the savings.for (@settings) { if ($boo < $_->[1]) { ($scalar, $msg) = @{$_}[2,3]; last; } }
'Windowing' or 'binning' operations like this can be done with a logarithmic number of comparisons, using a binary tree to store the limits. The added complexity of the code to support that is probably not justified in so small a set of bins as this.
After Compline,
Zaxo
In reply to Re: slow code help
by Zaxo
in thread slow code help
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |