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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |