Hello All,

I am a bit confused with the below benchmarks. I have read in multiple places, including a regex tutorial here, to avoid the /g modifier when it is not needed. This made sense to me in that it shouldn't have to continue searching once it has found a match, if the first match is what you're looking for.

The below metrics (in the code comments) don't seem to bare this out. Can anyone alleviate my confusion?

use Time::HiRes qw(sleep time); my $time = time; for (1..1_000_000) { my $str = '123456789'; my ($a,$b) = $str =~ m/(23)[^8]+(8)/g; # about 1.88 sec #my ($a,$b) = $str =~ m/(23)[^8]+(8)/; # about 2.15 sec #my ($a) = $str =~ m/(23)/g; # about 1.23 sec #my ($a) = $str =~ m/(23)/; # about 1.41 sec } say time - $time;

Edit: I'm running Strawberry 5.12.3


In reply to 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.