Speeding up a regular expression
Chris Liechti
cliechti at gmx.net
Tue Oct 23 14:39:18 EDT 2001
>> lines = string.split(inputString,"\n")
retlist = []
>> for line in lines:
>> if myRe.match(line):
retlist.append(line)
return '\n'.join(retlist) #newer python versions
## return string.join(retlist,'\n') #for py 1.5.2:
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().
you could also use "filter()", that moves the python for-loop to the fast
loop in C in filter:
return '\n'.join(filter(myRe.match, lines))
hope that helps
chris
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list