gives (where "foreach" is your code and "hash" is the above):sub sort_unique_hash { my %hash; @hash{@_} = (); return sort keys %hash; }
This is with:Benchmark: timing 100000 iterations of foreach, hash... foreach: 14 secs (13.50 usr 0.00 sys = 13.50 cpu) hash: 6 secs ( 5.85 usr 0.00 sys = 5.85 cpu)
The difference is even more marked if you have rather odd arrays that you want to sort -u. For example, running the code against this array:@list = qw/foo bar baz foo quack bar baz/;
gives@list = ('foo') x 1000;
Presumably because your code sorts the list before it selects only the unique elements, and the hash approach only sorts the unique list--so you're sorting 1000 elements, and the hash approach just sorts 1.Benchmark: timing 1000 iterations of foreach, hash... foreach: 12 secs (11.82 usr 0.00 sys = 11.82 cpu) hash: 1 secs ( 0.88 usr 0.00 sys = 0.88 cpu)
Just something to think about.
In reply to RE: RE: Return a Unique Array
by btrott
in thread Return a Unique Array
by Anonymous Monk
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |