Perl remove double entries in array Benchmark

Today I Benchmark two solution to remove all double entries from an array in Perl:

use strict;
use Benchmark qw(:all) ;

cmpthese(-1, {
'uniq1'  => sub {uniq1(1,1,2,2,3,4,5,6,6);},
'uniq2'  => sub {uniq2(1,1,2,2,3,4,5,6,6);},

sub uniq1 {
my %hash;
return (sort keys %hash);

sub uniq2 {
    my %seen;
    grep !$seen{$_}++, @_;

We see the second function is 18% faster than the other one:

uniq2 395476/s    --  -16% 
uniq1 468114/s   18%    --


Now we have a good fast solution to remove the double entries from our array:

use strict;
my @arr = (1,1,2,2,3,4,5,6,6);

print "@arr\n";
@arr = uniq(@arr);
print "@arr\n";

sub uniq {
my %hash;
return (sort keys %hash);

Our result:

1 1 2 2 3 4 5 6 6 
1 2 3 4 5 6

7 thoughts on “Perl remove double entries in array Benchmark”

  1. At this time I am ready to do my breakfast, once having my breakfast coming over
    again to read more news.

  2. You’ve made some good points there. I checked on the web for additional information about the issue and found most
    individuals will go along with your views on this website.

  3. Please let me know if you’re looking for a author for your site.
    You have some really great articles and I feel I would be
    a good asset. If you ever want to take some of the load off, I’d love to write
    some material for your blog in exchange for a link back to mine.
    Please shoot me an email if interested. Kudos!

  4. Hello, I wish for to subscribe for this web site to take latest
    updates, so where can i do it please assist.

  5. What’s up, yup this paragraph is really pleasant and I have learned lot of things
    from it about blogging. thanks.

Leave a Reply

Your email address will not be published.

4 + 8 =