Maybe it's a CPU cache thing. I'm just guessing because I expect the code path you're benchmarking to be identical.
I would guess it is because both the number AND the string representation are copied:

perl -MDevel::Peek -e'$x=42; "$x"; $y = $x; Dump $y' SV = PVIV(0x814e9a8) at 0x814d518 REFCNT = 1 FLAGS = (IOK,POK,pIOK,pPOK) IV = 42 PV = 0x8168e70 "42"\0 CUR = 2 LEN = 4
In summary - the slowdown effect should only be noticable when you're copying the variables: as you implied, using arithmatic on numbers that are also stringified should not slow down (much).

update: Ieronim, the issue is that when you stringify a number, perl stores both representations in the variable.

When you copy it, perl doesn't know whether you're going to use the copies as string or numbers, so it copies the whole variable (since that should be faster than copying only one representation and converting it later).


In reply to Re^2: Don't treat your numbers as strings, or Interpolation is worse than you might think by Joost
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.