in reply to Possessive Quantifiers in Perl 5.10 regexps
I have modified the code according to your comments. It is now as follows:
Hope there are no more bugs.pl@nereida:~/Lperltesting$ cat ./comparequotedstrings.pl #!/usr/local/lib/perl/5.10.1/bin//perl5.10.1 use v5.10; use Benchmark qw{cmpthese}; # See http://www.regex-engineer.org/slides/img10.html our $regexp = qr/ ["'] # double quote (?: # no memory [^"\\]++ # no " or escape: Don't backtrack | (?: \\.)++ # escaped character )*+ # Don't backtrack ["'] # end double quote /x; our $bregexp = qr/ ["'] # double quote (?: # no memory [^"\\]+ # no " or escape: backtrack | (?: \\.)+ # escaped character )* # backtrack ["'] # end double quote /x; # input matches many times. Using "g" option our $input = (q{"abc\"defg"hijk}x10000); cmpthese( 0, { gbacktrack => sub { $input =~ /$bregexp/g }, gpossessive => sub { $input =~ /$regexp/g } } ); #input matches a long string, no g option $input = '"'.(q{abc\"defg}x10000).'"'; cmpthese( 0, { backtrack => sub { $input =~ /$bregexp/ }, possessive => sub { $input =~ /$regexp/ } } ); # input does not match. Using "g" option $input = '"'.("abcdefghijk"x10000); cmpthese( 0, { failgbacktrack => sub { $input =~ /$bregexp/g }, failgpossessive => sub { $input =~ /$regexp/g } } ); # Input does not match. Force the nested parenthesis # to work. our $quotes = q{\\"}x30; $input = '"'.("abcdef $quotes ghijk"x1000); cmpthese( 0, { failgbacktracknested => sub { $input =~ /$bregexp/g }, failgpossessivenested => sub { $input =~ /$regexp/g } } );
Is the only case where the version with the possesive quantifiers wins.$input = '"'.("abcdefghijk"x10000);
Observe the huge difference in the final case (that also fails to match):
pl@nereida:~/Lperltesting$ ./comparequotedstrings.pl Rate gpossessive gbacktrack gpossessive 569399/s -- -14% gbacktrack 665929/s 17% -- Rate backtrack possessive backtrack 163/s -- -4% possessive 169/s 4% -- Rate failgbacktrack failgpossessive failgbacktrack 16.6/s -- -97% failgpossessive 583/s 3419% -- pl@nereida:~/Lperltesting$ ./comparequotedstrings.pl Rate gpossessive gbacktrack gpossessive 588574/s -- -15% gbacktrack 694595/s 18% -- Rate backtrack possessive backtrack 164/s -- -4% possessive 171/s 4% -- Rate failgbacktrack failgpossessive failgbacktrack 17.3/s -- -97% failgpossessive 583/s 3276% -- (warning: too few iterations for a reliable count) s/iter failgpossessivenested failgbacktrackne +sted failgpossessivenested 23.1 -- - +100% failgbacktracknested 4.42e-02 52042%
|
|---|