in reply to Re: Find unique elements in an array
in thread Find unique elements in an array
It's compact, but far and away the slowest method.
However, you can combine elements of yours with tilly's performance trick and get something that is both fairly compact and almost as quick.
#! perl -slw use strict; use Benchmark qw(:all) ; my @a; push @a, int (rand(100)) foreach 1..100_000; my( @jc, @mk, @bt, @buk, @etc ); my %tests = ( jc => sub { my %unique; foreach my $thingy (@a) { $unique{$thingy} = 1; } @jc = keys %unique; }, mk => sub { my %unique; @unique{ @a} = 1; @mk = keys %unique; }, bt => sub { my %unique; undef(@unique{@a}); @bt = keys %unique; }, etc => sub { @etc = keys %{{map {$_=>1} @a }}; }, buk => sub { @buk = keys %{ +do{ my %u; undef( @u{ @a } ); \%u } }; }, ); #cmpthese( 1, \%tests ); #print scalar @$_ for \( @jc, @mk, @bt, @buk ); cmpthese( -3, \%tests ); __END__ P:\test>324513 Rate etc jc mk buk bt etc 4.75/s -- -82% -94% -95% -95% jc 26.1/s 449% -- -65% -72% -72% mk 74.3/s 1463% 184% -- -21% -22% buk 93.6/s 1870% 259% 26% -- -1% bt 94.6/s 1891% 262% 27% 1% --
|
|---|