in reply to Re^6: Regex match at the beginning or end of string
in thread Regex match at the beginning or end of string
Okay. So the optimiser favours the lookaheads by a factor of 16x:
#! perl -slw use strict; use Benchmark qw[ cmpthese ]; use List::Util qw[ shuffle ]; our @terms = qw[ the quick brown fox jumps over the lazy dog ]; our $re = join'', map "(?=^.*$_)", @terms; $re = qr/$re/; our @lines = map join( ' ', shuffle @terms), 1 .. 100; push @lines, ( 'every good boy deserves food' ) x 100; our( $a, $b ) = (0) x 2; cmpthese -1, { a=>q[ /$re/ and ++$a for @lines; ], b=>q[ for my $str ( @lines ) { !grep( $str !~ /$_/, @terms) and ++ +$b; } ], }; print "$a:$b"; __END__ C:\test>junk48 Rate b a b 81.5/s -- -94% a 1399/s 1616% -- 208200:12000
Or, if I don't pre-compile the regex, 30x faster:
#! perl -slw use strict; use Benchmark qw[ cmpthese ]; use List::Util qw[ shuffle ]; our @terms = qw[ the quick brown fox jumps over the lazy dog ]; our $re = join'', map "(?=^.*$_)", @terms; #$re = qr/$re/; our @lines = map join( ' ', shuffle @terms), 1 .. 100; push @lines, ( 'every good boy deserves food' ) x 100; our( $a, $b ) = (0) x 2; cmpthese -1, { a=>q[ /$re/ && ++$a for @lines; ], b=>q[ for my $str ( @lines ) { !grep( $str !~ /$_/, @terms) && ++$ +b; } ], }; print "$a:$b"; __END__ C:\test>junk48 Rate b a b 82.7/s -- -97% a 2632/s 3082% -- 389700:12000
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Regex match at the beginning or end of string
by JavaFan (Canon) on Feb 20, 2011 at 15:46 UTC | |
by BrowserUk (Patriarch) on Feb 20, 2011 at 18:45 UTC | |
by JavaFan (Canon) on Feb 20, 2011 at 22:07 UTC |