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