Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Note that %combined = (%hash1, %hash2); suffers from performance penalty. If performance's a concern, it's always better to do map { $hash1{$_} = $hash2{$_} } keys %hash2; (unless you want to keep %hash1 and %hash2 intact, which doesn't seem to be the case in your question)

use Benchmark; my $size = 100000; my %hash1 = map { $_ => 1 } (0..$size); my %hash2 = map { $_ => 1 } ($size..(2*$size)); my $t0 = new Benchmark; my %combined = (%hash1, %hash2); my $t1 = new Benchmark; print "Combining took:",timestr(timediff($t1, $t0)),"\n"; $t0 = new Benchmark; map { $hash1{$_} = $hash2{$_} } keys %hash2; my $t1 = new Benchmark; print "Map took:",timestr(timediff($t1, $t0)),"\n";
When size = 100000 and 1000000 respectively, the results:
inq123@perlmonks$ perl test.pl
Combining took: 0 wallclock secs ( 0.28 usr +  0.02 sys =  0.30 CPU)
Map took: 0 wallclock secs ( 0.18 usr +  0.00 sys =  0.18 CPU)
inq123@perlmonks$ perl test.pl
Combining took:42 wallclock secs (41.53 usr +  0.21 sys = 41.74 CPU)
Map took: 2 wallclock secs ( 1.89 usr +  0.05 sys =  1.94 CPU)
So performance penalty is manifested when the hashs contain tens of thousands of elements, which is not too rare.

In reply to Re: Concerning hash operations (appending, concatenating) by inq123
in thread Concerning hash operations (appending, concatenating) by RazorbladeBidet

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (3)
    As of 2015-07-06 00:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (68 votes), past polls