A quick benchmark shows that fgrep is even about three times as fast as perl. (For 5.10.0, that is — with 5.8.8, the ratio is even worse... considerably!) Such a large difference is somewhat surprising (to me). I would've expected perl-5.10 to be about on par with fgrep. So, please, someone point out what I've done wrong in my benchmark... :)
perl-5.10.0:
Rate perl fgrep perl 30.8/s -- -71% fgrep 106/s 246% --
perl-5.8.8:
s/iter perl fgrep perl 9.38 -- -100% fgrep 3.00e-04 3124967% --
(In this particular reported case, none of the search words were found in $^X (the perl binary), so all strings had to be tested.)
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; }, });
In reply to Re^2: Searching multiple expressions in multiple Files (perl-5.10 vs. fgrep)
by almut
in thread Searching multiple expressions in multiple Files
by technoz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |