Title: RE: Query On Grep

you're absolutely right. That's what I get for coding without thinking. :-)

> -----Original Message-----
> From: Rhesa Rozendaal [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, February 04, 2004 6:42 PM
> To: Peter Eisengrein; [EMAIL PROTECTED]
> Subject: Re: Query On Grep
>
>
> Peter Eisengrein wrote:
>
> > using the below script (on my local drive, not across a network),
> > foreach'ing all files is approx 45% faster than grep'ing
> (at least on my
> > machine)
>
> Your benchmark is not fair, since you use two different conditions. A
> simple 'eq' is bound to be faster than a regex and a file test.
>
> Here's a better approach.
> I test both conditions against eachother. Not surprising, the grep is
> about as fast as the foreach; a bit faster for the 'eq', and a bit
> slower for the regex/file test.
>
> > #####
>
> [snip]
>
> > timethese(100000,
> {'allfiles'=>\&allfiles,'grepfiles'=>\&grepfiles});
> >
> > sub allfiles
> >                 if ($file eq $matchthis)
>
> [snip]
>
>  > sub grepfiles
> >         my @files = grep { /^$matchthis$/ && -f "$dir/$_" }
> readdir(DIR);
>
> My proposal:
>
> use strict;
> use Benchmark;
>
> my $dir = 'c:/temp';
> my $matchthis = 'q2';
>
> timethese(1000, {'allfiles'=>\&allfiles,'grepfiles'=>\&grepfiles});
> timethese(1000,
> {'allfiles1'=>\&allfiles1,'grepfiles1'=>\&grepfiles1});
>
> sub allfiles
> {
>          opendir(DIR,$dir);
>          my @files = readdir(DIR);
>          foreach my $file (@files)
>          {
>                  if ($file eq $matchthis)
>                  {
>                          1;
>                  }
>          }
>          closedir(DIR);
> }
>
> sub grepfiles
> {
>          opendir(DIR,$dir);
>          my @files = grep $_ eq $matchthis, readdir(DIR);
>          closedir(DIR);
> }
>
>
> sub allfiles1
> {
>          opendir(DIR,$dir);
>          my @files = readdir(DIR);
>          foreach my $file (@files)
>          {
>                  if (/^$matchthis$/o && -f "$dir/$_")
>                  {
>                          1;
>                  }
>          }
>          closedir(DIR);
> }
>
> sub grepfiles1
> {
>          opendir(DIR,$dir);
>          my @files = grep  /^$matchthis$/o && -f "$dir/$_" ,
> readdir(DIR);
>          closedir(DIR);
> }
>
>
>
> > ### RESULTS:
> >
> > Benchmark: timing 100000 iterations of allfiles, grepfiles...
> >   allfiles: 370 wallclock secs (184.03 usr + 181.79 sys =
> 365.82 CPU) @
> > 273.36/s
> >  (n=100000)
> >  grepfiles: 545 wallclock secs (318.99 usr + 222.64 sys =
> 541.63 CPU) @
> > 184.63/s
> >  (n=100000)
>
> My results:
>
> Benchmark: timing 10000 iterations of allfiles, grepfiles...
>    allfiles: 13 wallclock secs ( 7.34 usr +  5.36 sys = 12.70 CPU) @
> 787.59/s (n=10000)
>   grepfiles: 12 wallclock secs ( 5.56 usr +  5.64 sys = 11.20 CPU) @
> 893.18/s (n=10000)
>
> Benchmark: timing 10000 iterations of allfiles1, grepfiles1...
>   allfiles1: 15 wallclock secs ( 8.61 usr +  5.58 sys = 14.19 CPU) @
> 704.67/s (n=10000)
> grepfiles1: 16 wallclock secs ( 7.79 usr +  7.62 sys = 15.41 CPU) @
> 648.85/s (n=10000)
>
>
> Rhesa Rozendaal
>
> > ####
> >
> > -Pete
> >
> >
> >  > -----Original Message-----
> >  > From: Sourav G Ghatak [mailto:[EMAIL PROTECTED]]
> >  > Sent: Wednesday, February 04, 2004 7:49 AM
> >  > To: [EMAIL PROTECTED]
> >  > Subject: Query On Grep
> >  >
> >  >
> >  > I open a directory and start a grep on the contents for a
> >  > particular pattern
> >  > I open a directory read all contents and choose only the
> pattern...
> >  >
> >  > which of these is faster.... i want to know.. that to over
> >  > the network....
> >  > Any help will be greatly appreciated.
> >  >
> >  > Sourav.
> >  >
>

Reply via email to