Skip to content

Instantly share code, notes, and snippets.

@kba
Last active February 13, 2016 13:06
Show Gist options
  • Save kba/879f3cb33311f15c4d44 to your computer and use it in GitHub Desktop.
Save kba/879f3cb33311f15c4d44 to your computer and use it in GitHub Desktop.
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';
}
};
@kba
Copy link
Author

kba commented Feb 13, 2016

          Rate    re index
re    102354/s    --   -5%
index 107296/s    5%    --

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment