On Mon, 19 Nov 2001 23:33:32 +0000 (GMT), Richard Smith wrote: > I am trying to make >the script as efficient as possible (obviously), and I >have 2 versions of a routine that does a bitwise >comparison to match a filter. Can anyone tell me just >by looking which is likely to be fater? My guess is >the map version: > >1. loop version: > >for (@image_list) { > ( int( $filter ) & int( $image_data{$_}[2] ) ) == >$filter and push @match, $_; >} > >2. Map version: > > @match = map { ( int( $filter ) & int( >$image_data{$_}[2] ) ) == $filter and $_ or () } >@image_list;
I agree 100% with Keary, you should use grep() instead. But, and this might be disappointing to you: I expect all versions to be nearly the same speedwise. That is because internally, they all do the same number of loops, and all have almost identical loop bodies, too. If one of them were significantly slower than the others, somebody from the porters would have screwed up seriously. ;-) Oh, since $filter is unmodified throughout the loop, you just might as well make sure that it's an integer in front of the loop, and drop that explicit conversion to a number using int() inside the loop. If one of the operands passed to the "&" operator is numerical, that's good enough, the other one will be turned into a number when used by the operator. $filter += 0; @match = grep { ($filter & $image_data{$_}[2]) == $filter } @image_list; -- Bart.