Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Using an integer as a string usually makes for a considerably larger (in memory) scalar than using a string as a number.

How does this fit in with the size that Devel::Size reports? If you can trust it, a stringified number, and a numified string result in the same size (32 byte for the value 123, on a 32-bit Perl).

use Devel::Size qw(size); use Devel::Peek; sub info { print Dump($_[0]); print "size = ",size($_[0])," ($_[1])\n\n"; } $num = 123; # or int("123") info($num, "integer"); $num .= ""; info($num, "integer stringified"); $str = "123"; info($str, "string"); $str += 0; info($str, "string numified"); $str += 45678900; info($str, "... with bigger integer"); $str .= ""; info($str, "... re-stringified");

outputs something like

SV = IV(0x816983c) at 0x8192124 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 123 size = 16 (integer) SV = PVIV(0x8150b10) at 0x8192124 REFCNT = 1 FLAGS = (POK,pPOK) IV = 123 PV = 0x81c9f18 "123"\0 CUR = 3 LEN = 4 size = 32 (integer stringified) SV = PV(0x814fb90) at 0x81ca934 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x81950f0 "123"\0 CUR = 3 LEN = 4 size = 28 (string) SV = PVIV(0x8150b20) at 0x81ca934 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 123 PV = 0x81950f0 "123"\0 CUR = 3 LEN = 4 size = 32 (string numified) SV = PVIV(0x8150b20) at 0x81ca934 REFCNT = 1 FLAGS = (IOK,pIOK) IV = 45679023 PV = 0x81950f0 "123"\0 CUR = 3 LEN = 4 size = 32 (... with bigger integer) SV = PVIV(0x8150b20) at 0x81ca934 REFCNT = 1 FLAGS = (POK,pPOK) IV = 45679023 PV = 0x81950f0 "45679023"\0 CUR = 8 LEN = 12 size = 40 (... re-stringified)

Devel::Peek shows a comparable resulting structure for "number stringified" and "string numified" (with respect to IV and PV usage). Also, one can observe that the overall size gets larger if you make the number bigger, and then re-stringify the variable...

Anyhow, does your comment mean that Devel::Size is not reporting the size related to the entire PV buffer allocated for the cached stringified form, but rather its currently used part only (up to and including the \0)? — which would make it a less useful tool for determining real memory usage. Actually, the size that Devel::Size reports seems to be related to the LEN in the Devel::Peek dump (which itself you can observe to increment in steps of 4, if you play around a bit). Just wondering...


In reply to Re^2: Strings and numbers: losing memory and mind. (SV sizes) by almut
in thread Strings and numbers: losing memory and mind. by kyle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found