in reply to Re: multiple greps at once-- possible?
in thread multiple greps at once-- possible?

Since you're not actually using the set of values generated by each grep, but only want to test whether there is such a match, it might (depending on the sizes of the arrays and how often called) be useful to write your tests as a short-circuiting foreach:
Whether 'grep' or a short circuiting for is faster doesn't depend on how often you call it (although it matters whether it's significant). The size of the array plays a role, but what's more important is whether there is a match, and if there is, where the first match is. 'grep' wins if there's no match, or if the match is at the end. In the past, I've done some benchmarking, and found that the short circuit method was faster if the first match was to be found in about the first two-thirds of the list. I've also found noticable differences in the cut-off point when doing exact matching (==) or regex matching (=~) - differences I've not been able to explain.

But with different versions of Perl, you might get different results. My point is that deciding whether the replace a grep with a short-circuiting for loop is not an easy decision.

Abigail

Replies are listed 'Best First'.
Re: Re: multiple greps at once-- possible?
by ccn (Vicar) on Apr 08, 2004 at 20:49 UTC
    Hmm... It's intresting to combine the both approaches
    my @a = qw (a d c d s f ss eef e as d); GREP: { grep{ $_ eq 'd' and next GREP } @a; print 'not match'; last GREP; } continue { print 'match'; last GREP; }
    I'll benchmark this code later

    Update: It' was a bad idea. This code is very slow. The two fastest are:

    #... grep_line => sub { my($test, $ra) = @_; grep { $_ eq $t and return 1 } @$ra; return; }, for_line => sub { my($test, $ra) = @_; $test eq $_ and return 1 foreach @$ra return; } #...
Re: Re: multiple greps at once-- possible?
by Roy Johnson (Monsignor) on Apr 08, 2004 at 17:39 UTC
    Whether 'grep' or a short circuiting for is faster doesn't depend on how often you call it
    Correct, but whether it is usefully faster might.
    what's more important is whether there is a match
    Well, yes. I did assume that we would expect to find a match rather often.

    The PerlMonk tr/// Advocate