I'd like to see that benchmark. My version gives somewhat different results:

use strict; use warnings; use List::MoreUtils; use Benchmark qw(cmpthese); my @values = map {int rand 10} 1 .. 1000; for my $sub (\&UtilsM, \&UtilsH, \&Grep, \&Map, \&For,) { print join ("\t", sort $sub->(@values)), "\n"; } cmpthese (-1, { UtilsM => sub {UtilsM (@values)}, UtilsH => sub {UtilsH (@values)}, Grep => sub {Grep (@values)}, Map => sub {Map (@values)}, For => sub {For (@values)}, } ); sub UtilsM { return List::MoreUtils::uniq (@_); } sub UtilsH { my %h; return map { $h{$_}++ == 0 ? $_ : () } @_; } sub Grep { my %unique =(); grep {$unique{$_} = undef} @_; return keys %unique; } sub Map { my %unique =(); map {$unique{$_} = undef} @_; return keys %unique; } sub For { my %unique =(); $unique{$_} = undef for @_; return keys %unique; }

Prints (neglecting the sanity check output):

Rate Grep For UtilsH Map UtilsM Grep 5068/s -- -0% -6% -22% -63% For 5068/s 0% -- -6% -22% -63% UtilsH 5410/s 7% 7% -- -17% -61% Map 6502/s 28% 28% 20% -- -53% UtilsM 13863/s 174% 174% 156% 113% --

Update: UtilsM uses List::MoreUtils. UtilsH is the uniq code implemented in the same context as the other benchmark tests.


True laziness is hard work

In reply to Re^3: searching for unique numbers into a string by GrandFather
in thread searching for unique numbers into a string by marcelpreda

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.