This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: grep


At 16:30 +0100 11/1/97, Sascha Ziemann wrote:
>What is the fastest grep?
>
>I tried this:
>
>(use-modules (ice-9 slib))
>(require 'string-search)
>
>(define (grep search-string filename)
>  (letrec ((grep* (lambda()
>		    (let ((line (read-line)))
>		      (if (not (eof-object? line))
>			  (begin (if (substring? search-string line)
>				     (begin (display line)
>					    (newline)))
>				 (grep*)))))))
>    (with-input-from-file filename grep*)))
>
>But the performance is terrible:
>
>real    1m34.139s
>user    1m33.890s
>sys     0m0.220s
>
>compared to the normal grep:
>
>real    0m0.198s
>user    0m0.050s
>sys     0m0.090s

Wow, a factor of 475 difference.

I don't know if this will help, but grep is a fairly optimized string
search program.  I'd start by doing less needless work, such as consing the
entire file into strings with read-line.  In your code there are two loops,
one that reads a line into a string, and one that searches for the
search-string.  I think it would help to merge the loops so that both could
happen at once.