Speeding up a regular expression

Michael George Lerner mlerner at pacman.gpcc.itd.umich.edu
Tue Oct 23 15:05:16 EDT 2001


Chris Liechti <cliechti at gmx.net> wrote:
<snip>

> extending strings does require lots memory allocations and compying the 
> strings, a list is far more efficient when changes are made on it.

> if the data comes from a file you could also use f.readlines() and omit 
> split().

it originally came from a file, but i only care about a small piece of the
file, which i give to my function as a string.  i think passing in the
whole file and using readlines() would slow it down, but i'll check.

<snip>

> return '\n'.join(filter(myRe.match, lines))

yup .. someone else emailed me about using a list instead of extending the
string.  doing that and using filter makes it run fairly quickly.  i'm not
so worried about speeding up the regular expression now, but it would
still be interesting.

many thanks

-michael



More information about the Python-list mailing list