in reply to Mathching an array in regex
On my Linux box (5.8.8), this will produce: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 larger strings *or* larger comparison word arrays,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% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Mathching an array in regex
by mrpeabody (Friar) on Oct 15, 2007 at 04:22 UTC |