On Aug 24, 2004, at 9:14 AM, Gunnar Hjalmarsson wrote:

Isn't grep() "specific" enough?

Initially you said:

    $line = join(' ',@myarray);
    if ($line =~ /my string/) {
        some code
    }

The equivalent using grep() would be:

    if ( grep /my string/, @myarray ) {
        some code
    }

Ordinarily, I wouldn't even mention this, but since the conversation is about what is fastest...


The above is pretty much the textbook perfect example of an inefficient use of grep(). The reason is that we just want to know if @myarray contains ANY /my strings/, but grep() fetches them ALL. If the list contains 10,000 entries, and the first is a /my string/, that's 9,999 lookups we didn't need.

I would simply substitute List::Utils' first() in the above example, to "fix" this.

Though again, it probably isn't "broken" at all. (See my speed rant in previous message.)

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to