Thanks ikegami, Thanks moritz.

I have modified the code according to your comments. It is now as follows:

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 } } );
Hope there are no more bugs.
The possesive qualifier now wins in the first case where the input fails
$input = '"'.("abcdefghijk"x10000);
Is the only case where the version with the possesive quantifiers wins.

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%

In reply to Re: Possessive Quantifiers in Perl 5.10 regexps by casiano
in thread Possessive Quantifiers in Perl 5.10 regexps by casiano

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.