Skip to content

Instantly share code, notes, and snippets.

@mpeters
Created February 26, 2010 16:40
Show Gist options
  • Save mpeters/315886 to your computer and use it in GitHub Desktop.
Save mpeters/315886 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw(:all);
my %strings = (
three => [[qw(foo bar baz fut)], 'bar'],
five => [[qw(foodi barsi bazie foots)], 'foodi'],
seven => [[qw(fooding barsing bazieys footsys)], 'bazieys'],
nine => [[qw(foodinges barsinges bazieyses footsysin)], 'footsysin'],
);
foreach my $len (keys %strings) {
print "\nString of $len characters\n";
my $words = $strings{$len}->[0];
my $match = $strings{$len}->[1];
cmpthese(
100000,
{
'using eq' => sub {
my $count;
foreach my $word (@$words) {
if ($word eq $match) {
$count++;
}
}
},
'using a regex' => sub {
my $count;
foreach my $word (@$words) {
if($word =~ /^$match$/ ) {
$count++;
}
}
},
'using a quotemeta regex' => sub {
my $count;
foreach my $word (@$words) {
if($word =~ /^\Q$match\E$/ ) {
$count++;
}
}
},
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment