You're triggering substr's lvalue return, which involves magic.

use Devel::Peek; { my $subRef = \substr $string, 0; Dump($$subRef); } { my $subRef = \(''.substr $string, 0); Dump($$subRef); }
SV = PVLV(0x1834fdc) at 0x1831820 REFCNT = 2 FLAGS = (PADMY,GMG,SMG,pPOK) IV = 0 NV = 0 PV = 0x18208ec ""\0 CUR = 0 LEN = 4 MAGIC = 0x182443c MG_VIRTUAL = &PL_vtbl_substr MG_TYPE = PERL_MAGIC_substr(x) TYPE = x TARGOFF = 0 TARGLEN = 0 TARG = 0x236dc8 SV = PV(0x238e44) at 0x236dc8 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x182eca4 ""\0 CUR = 0 LEN = 4 SV = PV(0x238e80) at 0x1831808 REFCNT = 2 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x183646c ""\0 CUR = 0 LEN = 4

By changing to

my $subRef = \(''.substr $string, 0);

I get

0.0120670795440674 0.0105710029602051 0.010854959487915

Update: scalar also works, and doesn't have the (albeit minute) overhead of calling concat.

my $subRef = \scalar substr $string, 0;

In reply to Re: Access via substr refs 2000 times slower by ikegami
in thread Access via substr refs 2000 times slower by BrowserUk

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.