Calling
undef on a hash slice is faster still.
use Benchmark qw(:all) ;
my @a;
push @a, int (rand(10)) foreach 1..100;
my %unique;
my @awd;
cmpthese($1000, {
'jc' =>
sub { foreach my $thingy (@a) { $unique{$thingy} = 1
+; }
@awd = keys %unique;
},
'mk' =>
sub { @unique{ @a} = 1;
@awd = keys %unique;
},
'bt' =>
sub { undef(@unique{@a});
@awd = keys %unique;
},
});
gives me
Rate jc mk bt
jc 16556/s -- -54% -62%
mk 36080/s 118% -- -18%
bt 44007/s 166% 22% --
(Incidentally that is a truely bizarre indent style.)