Rate perl fgrep
perl 30.8/s -- -71%
fgrep 106/s 246% --
####
s/iter perl fgrep
perl 9.38 -- -100%
fgrep 3.00e-04 3124967% --
####
use strict;
use warnings;
use Benchmark qw(cmpthese);
my @letters = ('A'..'Z','a'..'z');
sub searchword {
my $len = shift;
return join '', map { @letters[int rand(@letters)] } 1..$len;
}
my @words = map { searchword(3+rand(20)) } 1..300;
my $strings_file = "list_of_strings_file";
open my $fh, ">", $strings_file or die "Couldn't write '$strings_file': $!";
print $fh "$_\n" for @words;
close $fh;
my $re = join '|', @words;
cmpthese(100, {
'perl' => sub {
open my $fh, "<", $^X or die "Couldn't open '$^X': $!";
my $content;
sysread $fh, $content, 1e7 or die $!;
close $fh;
$content =~ /$re/;
},
'fgrep' => sub {
system qw(grep -F -o -f), $strings_file, $^X;
},
});