Hmm. Do you remember this?
There is a possible caveat with this change! Your explicit loop does allow you to short circuit the loop at the high pass limit which isn't possible with grep. However, as shown above, using grep does allow you to avoid the creation of and later copying of the temporary array. This combined with greps inherent better performance may outweigh the benefit of that short circuiting. Or it may not.The only way to tell will be to make the change and benchmark. If the size of the array being filtered is sufficiently large, and the pass band sufficiently narrow and low down in the range that the short circuit is beneficial, then it would be better to use a binary search algorithm to discover the low and high range limits and then copy the values across to the results set using an array slice.
Try substituting this for the grep.
my $aref = $matches{ $fasta_id }{ $sitekey }; my( $lo, $hi ) = ( 0, scalar @{ $aref } ); ++$lo while $aref->[ $lo + 1 ] < $lowerlimit; --$hi while $aref->[ $hi - 1 ] > $upperlimit; @{ $sets{ $fasta_id }[ $setscounter ]{ $sitekey } } = @{ $aref }[ lo +.. $hi ];
If that improves your performance, then using a binary search (as mentioned before) to find the lower and upper bounds should improve it further. Though binary searches coded in Perl aren't always as effective as you might hope.
In reply to Re: Help tightening up a subroutine please
by BrowserUk
in thread Help tightening up a subroutine please
by mdunnbass
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |