use strict; use warnings; use Benchmark; my @strings; my @chars = 'a'..'z'; for (1..10) { my $len = int ( rand(100)) + 10; my $string; for(1 .. $len) { $string .= $chars[int ( rand(@chars) )]; } push @strings, $string; } print scalar @strings, " strings created !\n"; my $iterations = 10000; my $count = 0; timethese( $iterations, { "esskar" => sub { foreach (@strings) { my @chars = split //, $_; my %h = (); $h{$_}++ foreach @chars; $count += is_true($h{k}, $h{r}, $h{w}); } } } ); print "esskar: $count\n"; $count = 0; timethese( $iterations, { "pq" => sub { foreach (@strings) { $count += is_true( tr/k//, tr/r//, tr/w// ); } } } ); print "pq: $count\n"; sub is_true { my ($k, $r, $w) = @_; $k ||= 0; $r ||= 0; $w ||= 0; return $k >= 2 || $r == 2 && $w == 2 || $k == 1 && $r == 1 && $w == 2; } ^Z 10 strings created ! Benchmark: timing 10000 iterations of esskar... esskar: 15 wallclock secs (14.47 usr + 0.00 sys = 14.47 CPU) @ 691.13/s (n=10000) esskar: 60000 Benchmark: timing 10000 iterations of pq... pq: 0 wallclock secs ( 0.34 usr + 0.00 sys = 0.34 CPU) @ 29069.77/s (n=10000) (warning: too few iterations for a reliable count) pq: 60000