For this particular test I ran, hash assignment and slices seemed to be much faster than creating the union one item at a time:
use strict; use warnings; use Benchmark 'cmpthese'; my (%set_a, %set_b); @set_a{1..100} = undef; @set_b{50..150} = undef; sub slices { my %set; @set{keys %set_a, keys %set_b} = undef; } sub items { my %set; $set{$_} = undef for keys %set_a, keys %set_b; } sub hasheq { my %set; %set = (%set_a, %set_b); } cmpthese(10000, { slices => \&slices, items => \&items, hasheq => \&hasheq, });
gives these results on my machine:
Benchmark: timing 10000 iterations of hasheq, items, slices... hasheq: 3 wallclock secs ( 2.97 usr + 0.00 sys = 2.97 CPU) @ 33 +67.00/s (n=10000) items: 4 wallclock secs ( 4.18 usr + 0.00 sys = 4.18 CPU) @ 23 +92.34/s (n=10000) slices: 3 wallclock secs ( 2.96 usr + 0.00 sys = 2.96 CPU) @ 33 +78.38/s (n=10000) Rate items hasheq slices items 2392/s -- -29% -29% hasheq 3367/s 41% -- -0% slices 3378/s 41% 0% --

-- Mike

--
just,my${.02}


In reply to Re: Re: Fast Way to Combine Two Hashes by thelenm
in thread Fast Way to Combine Two Hashes by arunhorne

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.