in reply to Mathching an array in regex

narainhereHi monks, I would like to do a regex on a string, to see if it matches any of the strings in a array

After reading the solutions here, I was tempted to
post a second contribution (which I do now).

The topic is interesting because so many
ways exist to solve it. My question was then:
what are they worth in real code. Stressing the
point of the OP: "to see if it matches any of the strings in a array",
it's clear that the grep/map-solutions do too much here.
But what about the regex-only solution?

Here's my first attempt to get behind that:
use strict; use warnings; use List::Util qw'first reduce'; use Benchmark qw'cmpthese timethese'; my @arr = qw' cool guy here ' x 1; my $str = '100 WORDS ' x 50 . 'I am cool'; my @invocation=(0)x5; my $results = timethese(0, { 'word_altn' => sub { local $" = '|'; if( $str =~ /@arr/ ) { ++$invocation[0]; # print "Matched\n" } }, 'block_grep' => sub { if( grep { index($str, $_) !=-1 } @arr) { ++$invocation[1]; # print "Matched\n" } }, 'expr_grep' => sub { if( grep index($str, $_) !=-1, @arr) { ++$invocation[2]; # print "Matched\n" } }, 'list_util_index' => sub { if( first { index($str, $_) != -1 }, @arr ) { ++$invocation[3]; #print "Matched\n" } }, 'list_util_regex' => sub { if( first { $str =~ /$_/ }, @arr ) { ++$invocation[4]; #print "Matched\n" } } }, 'none' ); print "@invocation\n"; die unless 5 == grep $_>0, @invocation; cmpthese $results;
On my Linux box (5.8.8), this will produce:
42598 1157019 1349175 990717 1135313 Rate word_altn list_util_index list_util_regex blo +ck_grep expr_grep word_altn 10213/s -- -96% -97% + -97% -97% list_util_index 274237/s 2585% -- -8% + -16% -20% list_util_regex 299705/s 2835% 9% -- + -8% -13% block_grep 325087/s 3083% 19% 8% + -- -5% expr_grep 342754/s 3256% 25% 14% + 5% --
On larger strings *or* larger comparison word arrays,
the List::Util based solutions will tend to win
by a margin.

Regards

mwa

Replies are listed 'Best First'.
Re^2: Mathching an array in regex
by mrpeabody (Friar) on Oct 15, 2007 at 04:22 UTC
    The most idiomatic and most easily-understandable solution for "does a string contain any of these items" is:

        if ( grep { $str =~ /$_/ } @arr ) { ... }

    I ran the test again, with this new strategy added and a few other changes to the code:

    • $str and @arr are each longer
    • the matchword occurs in the middle of $str and @arr. This should make the solutions using first() more effective.
    • some of the test names are changed to better describe what they do

    Also, there is a small but important bug in your code:

      first BLOCK, ARRAY

    should be:

      first BLOCK ARRAY

    The former will only run the BLOCK once, regardless of outcome, and throw away the result. (Oh Perl syntax, why must you be so baroque?)

    So how does the new method fare in this adjusted test?

    word_altn 506/s grep_block_index 2356/s was: block_grep grep_expr_index 2373/s was: expr_grep grep_block_regex 3957/s <-- new method here first_index 4179/s was: list_util_index first_regex 6871/s was: list_util_regex
    The pleasant surprise is that a plain grep-regex solution turns out to be faster than replacing the regex with an index() call. Replacing grep() with first() gives a further speedup of about 75%, but as we've just seen you lose some maintainability, and that can be important.

    New code: