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

In reply to Re: Re: Find unique elements in an array by BrowserUk
in thread Find unique elements in an array by jcpunk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.