### Re: Re: Re: Find unique elements in an array

by kvale (Monsignor)
 on Apr 04, 2004 at 15:52 UTC ( #342480=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Find unique elements in an array
in thread Find unique elements in an array

If there is a stack penalty, it is not terrible, as the routine get faster with large arrays:
```use Benchmark qw(:all) ;

my @a;
push @a, int (rand(100)) foreach 1..2_000_000;

my %unique;
my (@awd1, @awd2, @awd3);
cmpthese(5, {
'jc' =>
sub { foreach my \$thingy (@a) { \$unique{\$thingy} = 1
+; }
@awd1 = keys %unique;
},
'mk' =>
sub { @unique{ @a} = 1;
@awd2 = keys %unique;
},
'ys' =>
sub { @unique{ @a} = ();
@awd3 = keys %unique;
},
});
yields
```Benchmark: timing 5 iterations of jc, mk, ys...
jc: 19 wallclock secs (16.75 usr +  0.35 sys = 17.10 CPU) @  0
+.29/s (n=5)
mk:  6 wallclock secs ( 6.00 usr +  0.01 sys =  6.01 CPU) @  0
+.83/s (n=5)
ys:  7 wallclock secs ( 6.00 usr +  0.00 sys =  6.00 CPU) @  0
+.83/s (n=5)
s/iter   jc   mk   ys
jc   3.42   -- -65% -65%
mk   1.20 185%   --  -0%
ys   1.20 185%   0%   --
The = () optimization does not seem to make much difference.

-Mark

Replies are listed 'Best First'.
Re: Re: Re: Re: Find unique elements in an array
by ysth (Canon) on Apr 04, 2004 at 19:53 UTC
With your original benchmark, I saw a consistent 4-5% increase for (). Obviously this is a constant difference that disappears into the woodwork with larger slices.
Re: Find unique elements in an array
by ccn (Vicar) on Apr 04, 2004 at 16:54 UTC
Anyway @unique{@a} = 1; looks a bit curious. Why do we set to 1 the only value in a huge hash?

Create A New User
Node Status?
node history
Node Type: note [id://342480]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2020-04-03 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The most amusing oxymoron is:

Results (27 votes). Check out past polls.

Notices?