I'm working with a hash containing approx 16 million key-value pairs; keys are short strings, values are integers.

When the hash is being generated I see memory usage rise to about 3.1GB. I have tried several ways to (sort) and print the hash but all of them seem to increase memory usage significantly. If I could avoid this increase then obviously I could work with a much bigger hash. Thanks in advance for any suggestions, or explanations.

Here's what I've tried...

print %hash;

This added 1.6GB to memory usage and took about 6 minutes to execute, but gives unhelpful output format.

my @keys = sort { $hash{$b} <=> $hash{$a} } keys %hash; foreach my $key (@keys) {print "$key\t$hash{$key}\n";}

This added 2GB to memory usage and took about 7 minutes, giving perfect sorted output for me.

print map { "$_ $summedNgrams{$_}\n" } keys %summedNgrams;

This added 3GB to memory usage and took about 8 minutes to give readable but unsorted output.

Running Perl on Cygwin64, Windows7. Timings approximate. Memory usage observed in Windows Task Manager.


In reply to What is the most memory efficient way to (sort) and print a hash? by a_salway

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.