That's what it is. Regex matching makes a copy of the scalar for use by $&, $1, etc. It uses the COW mechanism if possible. It doesn't for OOK scalars.

After one pass of test1's loop:

SV = PVMG(0x5bb22b8c3b80) at 0x5bb22b9beae8 REFCNT = 1 FLAGS = (SMG,POK,IsCOW,pPOK) <-- IsCOW: String buffer is shared IV = 0 NV = 0 PV = 0x5bb22b9ea230 "..." CUR = 99999 LEN = 100001 COW_REFCNT = 1 MAGIC = 0x5bb22b9c48f0 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_FLAGS = 0x40 BYTES MG_LEN = -1

After one pass of test2's loop:

SV = PVMG(0x5bb22b8c3bb0) at 0x5bb22b9cc1f0 REFCNT = 1 FLAGS = (SMG,POK,OOK,pPOK) <-- !IsCOW: String buffer isn't shared IV = 0 NV = 0 OFFSET = 1 PV = 0x5bb22ba028e1 ( "\x01" . ) "..." CUR = 99999 LEN = 100001 MAGIC = 0x5bb22b9c48f0 MG_VIRTUAL = &PL_vtbl_mglob MG_TYPE = PERL_MAGIC_regex_global(g) MG_FLAGS = 0x40 BYTES MG_LEN = -1

That means a copy of the string buffer must have been made. By the end of the loop, a copy has been done 99,999 times (once per successful regex match).

You can run into the same issue with foreign string buffers (e.g. a memory-mapped file). See why is Perl File::Map so slow compared to File::Slurp?


In reply to Re^2: Are beheaded strings known to be slow? by ikegami
in thread Are beheaded strings known to be slow? by Anonymous Monk

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.