The /o modifier isn't doing anything. Try running with and without it; when I did, there wasn't any difference. That modifier only affects patterns that have a variable interpolated in them, and your patterns don't.

The /g modifier seems to be the one that's making the difference, but I don't see why. I'd actually expect it to work the opposite way from what it does.

You might want to see also No More Meaningless Benchmarks! The operations involved are ridiculously fast, so I'm not sure how useful (or accurate) it is to compare them. Consider:

use Benchmark qw( cmpthese ); cmpthese( 10_000_000, { lower => sub { 'network' =~ /^network$/ }, upper => sub { 'networK' =~ /^networK$/ } } ); __END__ Rate lower upper lower 3174603/s -- -9% upper 3496503/s 10% --

Matching uppercase is faster than lowercase? Seriously?

I tried comparing literally the same subs, and there was still a 1% difference.

Adding an explicit scalar context brought the difference down a bit, but I'm not sure because the results aren't very consistent. In fact, I'd call them downright erratic.

All that being said, if someone can explain why a /g would make the pattern faster, I'd be very interested to hear. As it stands, I think there isn't a meaningful or consistent difference.


In reply to Re: Why this code run faster? by kyle
in thread Why this code run faster? by Gangabass

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.