Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Re: Find unique elements in an array

by BrowserUk (Pope)
on Apr 05, 2004 at 03:09 UTC ( #342559=note: print w/replies, xml ) Need Help??


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

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2020-02-19 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (81 votes). Check out past polls.

    Notices?