Created
February 26, 2010 16:40
-
-
Save mpeters/315886 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
#!/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