Well, you might think it shouldn't make much of a difference, but perl happens to disagree with you. I disagree with you too, but it's probably perl that you'll rather listen to. :)

You see, there's only one way it could match, but there are plenty of ways it can fail (as in "almost succeed"). And a regex engine has a tendency to try them all, it's apparently not yet smart enough to know it can't succeed. Looking at the regex

/(?:[^"]+|"")*/
and the string "around", there's plenty of ways this could possibly match:
(around) (aroun)(d) (arou)(nd) (arou)(n)(d) ...
Each phrase between parens indicates a group as matched by the subpattern [^"]+, and all groups are matched as a whole by * — as you can see, there are many ways it can be split up.

If you use the "cut" operator, there's only one way this can match:

(around)
So it makes sense to expect that using the cut operator might yield a significant speed boost.

But, without further ado, here's the benchmark, as run with perl 5.8.8:

my $s = q("You spin me ""around"" and ""around"", ""round"", like a re +cord, ""round"" and ""around"".); use Benchmark 'cmpthese'; cmpthese(-3, { cut => sub { return $s =~ /"(?>[^"]+|"")*"(?!")/ }, straight => sub { return $s =~ /"(?:[^"]+|"")*"(?!")/ } });
Results (neither the figures, nor their ratios), are not always the same, so take with a grain of salt:
Rate straight cut straight 7579/s -- -89% cut 69921/s 823% --
That's a factor of 9, that /(?>[^"]+|"")*/ is faster than /(?:[^"]+|"")*/, for this string, and that is definitely not insignificant.

In reply to Re^4: Help with Double Double Quotes regular expression (imprecise) by bart
in thread Help with Double Double Quotes regular expression by mattford63

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.