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

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊


In reply to Re: Don't treat your numbers as strings, or Interpolation is worse than you might think by diotalevi
in thread Don't treat your numbers as strings, or Interpolation is worse than you might think by Ieronim

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.