My rule of thumb is that each scalar takes roughly 30 bytes, minimum. This seems to agree well with your observations.

In case you were to try storing integers in a 2D array, may I tell you to look into vec(), as a way to store a one-dimensional array of integers in one scalar? The memory overhead would then become pretty much as you calculated, wasting only 30 bytes per row, while you still have handy single cell access. In case you're new to vec(): it is very much like what substr() is for character strings, except that now it's for arrays of integers, and you only have access to one individual cell at a time. You do have to specify the cell size with every vec() call.

Just to make it perfectly obvious: for a 2D array, you make a 1D (perl) array of such vec scalars.

But, since you really seem to be into floating point numbers, I won't. Take the other people's advice, and first of all, look into PDL, the number crunching suite for Perl.

Well... you could still use pack()/unpack() to store a 1D array of floating point numbers in a string, but it doesn't look very handy to me. You might as well write the stuff in C. ;-)


In reply to Re: why does this array take up so much memory? by bart
in thread why does this array take up so much memory? by Anonymous Monk

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.