Darrell news at
Wed May 2 07:24:19 EDT 2001

This might be faster. No time to check.

#!/bin/env python

# like pgrep but a python one.
# XXX Current about 2x slower than the Perl version :-(.

import sys, re, glob

def main():
    prog = re.compile(sys.argv[1])
    progSearch = # attempted performance improvement\
    for filename in sys.argv[2:]:
        for fn in glob.glob(filename):
            fp = open(fn, 'r')
            while 1:
                buf =
                if buf=='':
                mo = progSearch(buf)
                if mo:
                    print "%s:%d:%s" %(fn, lineno,

if __name__ == "__main__": main()

<Steven_Shaw at> 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
> 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