in reply to Don't treat your numbers as strings, or Interpolation is worse than you might think
It's an optimization. You asked perl to generate the string form of those number and it cached that. It shouldn't be any slower to access because both values are known to perl to have a valid number so it just uses the existing stored number. The upgrade caused your values in increase in size. Maybe it's a CPU cache thing. I'm just guessing because I expect the code path you're benchmarking to be identical.
In the following examples I've slimmed down the output a bit by removing REFCNT, CUR, LEN, and addresses. They didn't add anything but clutter. Run the code yourself if you want to see the extra goop.
A plain number. IOK says this has a valid number value stored.
perl -MDevel::Peek -e '$x=42;Dump $x' SV = IV(0x ) at 0x FLAGS = (IOK,pIOK) IV = 42
A stringified number. IOK and POK say it has valid integer and string values.
perl -MDevel::Peek -e '$x=42;print "$x";Dump $x'' SV = PVIV(0x ) at 0x FLAGS = (IOK,POK,pIOK,pPOK) IV = 42 PV = 0x "42"\0
A plain string. POK says it has a valid string value.
perl -MDevel::Peek -e '$x="42";Dump $x' SV = PV(0x ) at 0x FLAGS = (POK,pPOK) PV = 0x "42"\0
Watch the upgrade in action. Notice that the IV(0x....) address changed to PVIV(0x...). This tells you that the guts are now living somewhere else in memory. The old location wasn't big enough to be used as-is so it was copied to a new location (the old location made available again). The "at 0x813ab4c" that remained constant told you it was the same value as far as your user code could tell. You never knew any magic had happened.
perl -MDevel::Peek -e '$x=42;Dump $x;print NOWHERE "$x";Dump $x' SV = IV(0x815544c) at 0x812ab4c FLAGS = (IOK,pIOK) IV = 42 SV = PVIV(0x812c080) at 0x812ab4c FLAGS = (IOK,POK,pIOK,pPOK) IV = 42 PV = 0x8150890 "42"\0
⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊
|
|---|