Okay, let me try responding to this again. Blow by blow this time.

I hope perl is not so inefficient that it copies all the content of a list when it needs to process it, when simply pointing to the already allocated elements would be enough.
Even more so with arrays.

Um. The example contains arrays. So if the previous sentence was not talking about arrays, what was it talking about?

I'd also expect some COW mechanism to remove a lot of allocating and copying.

CopyOnWrite generally applies to entire data segments of a process that is cheaply shared with another process; that's obviously not applicable here.

Perl does have some internal flags and references with "COW" in their names; where by the copying of scalars is avoided (by aliasing) until and unless they are written to; but as the argument lists (destination & source) to op_aassign are inherently readonly, that does not apply here either.

Keys might be more trouble though, as all data stored in them have to be stringified first (and I wouldn't be surprised to learn that hash keys always hold a copy of the initial string, since as far as I can tell they are not standard scalar values).

Since CoW is not applicable to the destination and source lists; most of that is irrelevant, but I can confirm that hash keys are not normal scalars, and even if they are already in memory as scalars, the text will be copied into the HV structure.

I agree that the memory usage, and number of copies is certainly higher when you go all the way to slicing, but I don't expect "at least 4 times more" memory.

For the statement: @hash{ @keys } = @array; here is the memory usage:

C:\test>p1 print mem;; 9,356 K $#keys = 10e6; $#values = 10e6; $keys[ $_ ] = "$_", $values[ $_ ] = $_ + for 0 .. 10e6;; print mem;; 2,000,088 K @hash{ @keys } = @values;; print mem;; 4,394,716 K print size( \%hash );; 783106748

So, final memory usage: 4,394,716 K - initial memory usage: 9,356 K = memory used by the two arrays, the final hash and all the intermediate allocations for the stack, smaller versions of the hash during construction and other bits & bobs: 4,385,360 K or 4490608640 bytes.

And, 4490608640 / 783106748 = 5.734350586901084908030954676437. Your expectations are wrong.

I can't see any value going further.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^5: Does "preallocating hash improve performance"? Or "using a hash slice"? by BrowserUk
in thread Does "preallocating hash improve performance"? Or "using a hash slice"? by vr

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.