#! perl -slw use strict; use Benchmark qw[ cmpthese ]; our $words = do{ local( @ARGV, $/ ) = 'words.txt'; <> }; close $ARGV; our @words = split ' ', $words; our %words; @words{ do{ my $word = $_; map{ my $copy; substr( $copy = $word, $_, 1, '.' ); $copy; } 0 .. length() -1 }} = ($_) x ( length() -1 ) for @words; our $N //= 3; our $P //= 0.01; our @toLookFor = map { rand() > $P ? () : do { my $w = $_; my $p = int( rand length()-1 ); $w =~ s[.{$p}\K.][.]; $w; }; } @words; printf "Looking for %d terms amongst %d words\n", scalar @toLookFor, scalar @words; cmpthese $N, { a => q[ for my $re ( @toLookFor ) { m[^$re$] #and print "a:$re :: $_" for @words; } ], b => q[ $words =~ m[\b($_)\b] #and print "b:$_ :: $1" for @toLookFor; ], c => q[ # print $words{ $_ } or 1 for @toLookFor; ], } __END__ C:\test>junk42 -P=0.02 Looking for 3651 terms amongst 178691 words s/iter a b c a 167 -- -95% -100% b 8.20 1931% -- -100% c 1.00e-015 16662499999999995902% 820299999999997696% --