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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |