Hello, I have two snippets of code which get the intersection of two hash arrays and am wondering about performance on large scale data.

In a nutshell, what kind of performance is to be expected using grep vs looping through hashes? looking at the grep source code, it has a lot of buffering, I've tried to find some details on internals of memory allocation of hashes but ???

I read this "This does mean that hashes eat up a I<lot> of memory, both in memory Devel::Size can track (the memory actually in the structures and strings) and that it can't (the malloc alignment and length overhead)." from here http://cpansearch.perl.org/src/TELS/Devel-Size-0.71/lib/Devel/Size.pm

or perhaps is it possible to unroll the loop and thread it?

my %inter; for (keys %hist1) { if (exists $hist2{$_}) { my $val1 = $hist1{$_}; my $val2 = $hist2{$_}; $inter{$_} = ($val1 <= $val2) ? $val1 : $val2; } }
my @common = grep exists $hist1{$_}, sort keys %hist2;

Thanks for any insight.


In reply to memory:: grep vs looping through hash by f77coder

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.