Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Concerning hash operations (appending, concatenating)

by inq123 (Sexton)
on Mar 24, 2005 at 18:20 UTC ( #442145=note: print w/replies, xml ) Need Help??

in reply to Concerning hash operations (appending, concatenating)

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
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
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.

Replies are listed 'Best First'.
Re^2: Concerning hash operations (appending, concatenating)
by tilly (Archbishop) on Mar 26, 2005 at 02:10 UTC
    General advice. Just because there are many ways to do it in Perl is not a reason for picking a less readable one. If you are not returning data from map, then you should write it like this:
    $hash1{$_} = $hash2{$_} for keys %hash2;
    That is easier to read, and much more clearly signals intent. It is also at least as fast as the map version. (It used to be a lot faster, but in Perl 5.8 there is an optimization that causes map to shortcircuit to become a for if it is in null context.)

    Furthermore performance is far less likely to matter than most people think, and when it does having micro-optimized as you went is generally a bad strategy for getting it. (You want to keep code clean and then look for a better algorithm, or move a small section into C.) Therefore I would generally use the following strategy because it is even clearer, even though it is marginally slower on my machine (about 10% so):

    @hash1{keys %hash2} = values %hash2;
    And, of course, in the rare case that performance really mattered and I really wanted to work in Perl, it is fastest to avoid having to do 2 sets of hash lookups on %hash2:
    $combined{$k} = $v while my ($k, $v) = each %hash2;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://442145]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2023-09-28 20:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found