The 70mb it slurps is due to the @x array alone; I added a getc() to be able to check. I also added a third method - with interesting results.
#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); my @x = map join(',', split(//, rand(10000))), 0..80000; my %y; print "Check memory now.", $/; getc(); cmpthese(20, { map => sub { undef %y; %y = map(($_ => undef), @x) } for => sub { undef %y; @y{$_} = undef for @x; }, slice => sub { undef %y; @y{@x} = undef }, }); print scalar %y,$/; __END__ Check memory now. Benchmark: timing 20 iterations of for, map, slice... for: 16 wallclock secs (15.47 usr + 0.27 sys = 15.74 CPU) @ 1 +.27/s (n=20) map: 28 wallclock secs (26.67 usr + 0.30 sys = 26.97 CPU) @ 0 +.74/s (n=20) slice: 14 wallclock secs (13.54 usr + 0.24 sys = 13.78 CPU) @ 1 +.45/s (n=20) Rate map for slice map 0.742/s -- -42% -49% for 1.27/s 71% -- -12% slice 1.45/s 96% 14% -- 59853
Update: had mixed up the for and slice labels. for doesn't win.

Makeshifts last the longest.


In reply to Re: constructing large hashes by Aristotle
in thread constructing large hashes by duelafn

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.