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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.