Last active
February 13, 2016 13:06
-
-
Save kba/879f3cb33311f15c4d44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Benchmark qw(:all); | |
my $alphabet = 'STKPWHRAO*#EUFRPBLGTS'; | |
my $re = qr/^S?T?K?P?W?H?R?A?O?\*?#?E?U?F?R?P?B?L?G?T?S?\.?$/; | |
sub match_re { | |
my $yesno = ($_[0] =~ $re) ; | |
#print $_[0] . " " . ($yesno ? 'yes' : 'no') . "\n"; | |
return $yesno; | |
} | |
sub match_index { | |
my $bad_word; | |
my $last_pos = -1; | |
foreach my $ch (split '', $_[0]) { | |
$last_pos = index($alphabet, $ch, $last_pos + 1); | |
if ($last_pos < 0) { | |
$bad_word = 1; | |
last; | |
} | |
} | |
# print $_[0] . " " . ($bad_word ? 'no' : 'yes') . "\n"; | |
return $bad_word; | |
} | |
cmpthese 1_000_000, { | |
're' => sub { | |
match_re 'KAT'; | |
match_re 'FRAG'; | |
match_re 'TKPWAUL'; | |
match_re 'GAUL'; | |
match_re 'SAS'; | |
match_re 'SASS'; | |
}, | |
'index' => sub { | |
match_index 'KAT'; | |
match_index 'FRAG'; | |
match_index 'TKPWAUL'; | |
match_index 'GAUL'; | |
match_index 'SAS'; | |
match_index 'SASS'; | |
} | |
}; | |
Author
kba
commented
Feb 13, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment