#Bob,
Hanno,

Just try this... ;-)

I'm trying to get rid of 0, empty, undef, too.
You win for uniq though. I speeded up mine a
little by discovering I didn't need the grep
to delete null and zero and empty items. Can
you make yours do that, too?

#!/usr/bin/perl -w
use strict;
use Benchmark;
#my @list = qw(a b c d e f g h i j k l 1 2 3 1 2 4 m n o p q r s t u v w
#x y z a e f h j a z);

my @list = ( qw(a b c d e f g h i j 0 k l 1 2 3 1 2 4 m n o 0 p q r s t u v w
x y z a e f h j a z) , , 0 , '' , '' ) ;

sub uniq_nick { my %u = (); grep { ! ($u{$_}++) } @_; }
sub uniq_hah { grep { !(${_}{$_}++) } @_; }
sub uniq_bob { my %u = () ; for ( @_ ) { $_ and $u{$_} = 1 } ;
keys %u if ref \%u };
sub uniq_bob2 { grep { $_ ne $/ and $/ = $_ } sort grep {
$_ } @_ ; }
sub uniq_bob3 { grep { $_ ne $/ and $/ = $_ } sort @_ ; }

timethese( 1_000_000, { # better set to 10_000_000
'nick'=> 'uniq_nick(@list)',
'hah' => 'uniq_hah(@list)',
'bob' => 'uniq_bob(@list)',
'bob2' => 'uniq_bob2(@list)',
'bob3' => 'uniq_bob3(@list)',
});
print 'hah: '.join(",", uniq_hah(@list))."\n";
print 'bob: '.join(",", uniq_bob(@list))."\n";
print 'bob2: '.join(",", uniq_bob2(@list))."\n";
print 'bob3: '.join(",", uniq_bob3(@list))."\n";
print 'nick: '.join(",", uniq_nick(@list))."\n";

__END__

Mine is still slower but it does not need the
grep to strip out the 0's,empties, undefs.

Reply via email to