#!/usr/bin/perl -w use strict; use Benchmark 'cmpthese'; cmpthese 150, { 'merlyn' => sub { use Inline C =>; open (DATA,"data") or die "\nUnable to open data file\n"; while () { my $result = UniqueCount($_); } }, 'L~R' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { my %unique; scalar grep { !$unique{$_}++ } split //, $_; } }, 'OM_Zen' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { my $count; my %hashofuniq; my @txt1 = split ('',$_); @hashofuniq{@txt1} = 1; foreach (keys %hashofuniq){ $count++; } } }, 'rob_au' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { my %hash; @hash{ split '', $_ } = 1; scalar keys %hash; } }, 'pfaut' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { my $l = ''; my $u; for (sort split(//,$_)) { $u++,$l=$_ if ($_ ne $l); } } }, 'Coruscate' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { my %same = map { $_, 1 } split //, $_; scalar keys %same; } }, 'tall_man' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { $_ = join('', sort(split //,$_)); $_ =~ s/(.)\1+/$1/g; my $result = length($_); } }, 'Br_Uk-1' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { $_ = join('', sort(split //,$_)); $_ =~ tr/\x00-\xff//s; my $result = length($_); } }, 'Br_Uk-2' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { my @uniq; @uniq[unpack 'U*',$_] = (1)x length $_; scalar (grep defined $_, @uniq); } }, 'Br_Uk-3' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { my @uniq; @uniq[unpack 'C*',$_] = (1)x length $_; scalar (grep defined $_, @uniq); } }, 'Br_Uk-4' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { my @uniq; scalar grep{ ++$uniq[$_] == 1 } unpack('C*',$_); } }, 'Br_Uk-5' => sub { open (DATA,"data") or die "\nUnable to open data file\n"; while () { @_ = $_; scalar grep{ ++$_[$_] == 1 } unpack('C*',$_[0]); } }, 'hgolan30' => sub { use Data::Dumper; open (DATA,"data") or die "\nUnable to open data file\n"; while () { my @tmp=split('',$_); my %count; for(@tmp){ $count{$_} +=1; } Dumper \%count; } }, }; __END__ __C__ int UniqueCount(unsigned char *str) { char counts[256]; int i; int result; /* clear the array */ for (i = 0; i <= 255; i++) { counts[i] = (char) 0; } /* notice the characters */ while (*str) { counts[*str++] = 1; } /* now count the results */ result = 0; for (i = 0; i <= 255; i++) { result += counts[i]; } return result; }