pygrep
Darrell
news at dorb.com
Wed May 2 07:24:19 EDT 2001
This might be faster. No time to check.
--Darrell
#!/bin/env python
# like pgrep but a python one.
# XXX Current about 2x slower than the Perl version :-(.
import sys, re, glob
def main():
maxBuf=10000000
prog = re.compile(sys.argv[1])
progSearch = prog.search # attempted performance improvement\
lineno=0
for filename in sys.argv[2:]:
for fn in glob.glob(filename):
fp = open(fn, 'r')
while 1:
buf = fp.read(maxBuf)
if buf=='':
break
mo = progSearch(buf)
if mo:
print "%s:%d:%s" %(fn, lineno,
buf[mo.start():mo.end()+80])
fp.close()
if __name__ == "__main__": main()
<Steven_Shaw at adc.com> wrote >
>
> Hi there,
>
> I have converted an old Perl4 script to Python2.1. It is a simple tool to
> replace grep. My new Python script currently runs about twice as slow as
the old
> Perl version. I've tried afew things from the Python FAQ, but nothing
seems to
> improve things much. Can anyone suggest performance improvements for this
code?
>
> Here's the original Perl source code (pgrep):
>
> (See attached file: pgrep)
>
> Here's the Python source code (pygrep):
>
> (See attached file: pygrep)
>
> cheers,
>
> Steve.
>
More information about the Python-list
mailing list