in reply to Re: Re: Generating a grep command at run time
in thread Generating a grep command at run time

hardburn,
I have to completely disagree with you. The *nix utility grep is a compiled C program specifically designed for a single task. I worked very hard to modify tcgrep to be nearly as fast as the *nix grep. This only worked because of my very specific problem and still failed miserably in a general sense.

I am not sure what you mean by read normally instead of line-by-line. As best as I can tell, both Perl and grep see the file as a data stream. They read data into a buffer, they then process that buffer "line-by-line" by terminating on the newline character. The speed difference would be if the default buffer size is different - i.e. actual less physical reads as they only go back out to disk when the buffer is empty.

I agree that Perl offers all the functionality of the *nix grep and more, so resorting to using it for any reason other than raw speed seems like a bad idea. I would say that the reason not to use it isn't because of security or maintainability even though these are very valid points. To me, it is because of portability. This isn't even a matter of Win32 vs *nix portability - as can be seen in this thread.

Cheers - L~R

  • Comment on Re: Re: Re: Generating a grep command at run time

Replies are listed 'Best First'.
Re: Re: Re: Re: Generating a grep command at run time
by chimni (Pilgrim) on May 27, 2003 at 05:57 UTC
    Hi limbic-region
    thanks for your assistance.I finaly got the job done
Re: Re: Re: Re: Generating a grep command at run time
by hardburn (Abbot) on May 22, 2003 at 17:14 UTC

    I am not sure what you mean by read normally instead of line-by-line.

    I mean doing read(FH, $buf, $size); instead of my $buf = <FH>;. The plain read() call is faster than line-by-line reading, though generally harder to program around for many practical purposes. Most of the time, disk access will be a greater factor than the method of reading the file, so it's not something that I lose sleep over.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      hardburn,
      Your example is just changing the default buffer window - it is still reading it exactly as I have described. This is not faster than $var = <FH>; if you set the buffer smaller than the default. The only advantage speed wise to this method is if you do not care about newlines, which it appears that chimni is interested in. Now, it is possible to use a large buffer and write your own newline handler for reading through the buffer - you have to be sure to prepend whatever is left over on the last "line" on the next read to make sure lines that split are handled correctly. This seems like a lot more work to me - if you need that much speed - use the *nix grep.

      Cheers - L~R