No matter how long I run it for, it is remarkably consistent here with no more than 1 or 2% variation:

C:\test\perl-5.14.0-RC1>perl use Benchmark qw[ cmpthese ];; print $];; $str = '123456789'; cmpthese -10, { a=>q[ my ($a,$b) = $str =~ m/(23)[^8]+(8)/g; ], b=>q[ my ($a,$b) = $str =~ m/(23)[^8]+(8)/; ], c=>q[ my ($a) = $str =~ m/(23)/g ], d=>q[ my ($a) = $str =~ m/(23)/; ], };; ^Z 5.014000 Rate b a d c b 357543/s -- -15% -33% -45% a 422192/s 18% -- -21% -35% d 535621/s 50% 27% -- -18% c 653518/s 83% 55% 22% --

One difference of note is that I'm using Window rather than your Linux. Your results reflect ikegami's, who I believe was also using Linux. Perhaps the OP is on Windows?

The 'usual suspect' for performance differences a between those two is memory allocation, but there is none worthy of note here. Indeed, there appear (as you would suspect), to be no calls at all into the OS during benchmark.

Since were both on 64-bit intel hardware, that doesn't seem likely as a cause. Which pretty much leaves only compiler differences, with teh tentative conclusion that with the /g switch enabled, the Windows takes a code path that causes (or allows) the MSC compiler to generate a particularly efficient piece of code somewhere.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: Why does global match run faster than none global? by BrowserUk
in thread Why does global match run faster than none global? by Kc12349

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.