use strict;
use warnings;
use Benchmark qw/cmpthese/;
my @list1 = qw/a.f c data g j/;
my @list2 = qw/myfile.txt a.file.txt data.txt otherfile.txt jargon.txt/;
my $regex = join '|', map "\Q$_\E", @list1;
my $regexQR = qr/^(?:$regex)/;
sub theGrepQR {
my @list3 = grep $_ !~ $regexQR, @list2;
}
sub theGrepNoQR {
my @list3 = grep !/^(?:$regex)/, @list2;
}
sub theGrepOModifier {
my @list3 = grep !/^(?:$regex)/o, @list2;
}
sub thePush {
my @list3;
for my $match (@list1) {
push @list3, grep { substr( $_, 0, length($match) ) eq $match } @list2;
}
}
cmpthese(
-5,
{
theGrepQR => sub { theGrepQR() },
theGrepNoQR => sub { theGrepNoQR() },
theGrepOModifier => sub { theGrepOModifier() },
thePush => sub { thePush() },
}
);
__DATA__
Rate thePush theGrepQR theGrepNoQR theGrepOModifier
thePush 174874/s -- -41% -56% -67%
theGrepQR 296601/s 70% -- -26% -44%
theGrepNoQR 398394/s 128% 34% -- -25%
theGrepOModifier 533505/s 205% 80% 34% --