Which is faster...find or re. ?

Fearless Freep joconnor at cybermesa.com
Fri Aug 2 19:48:16 CEST 2002


I'm doing some parsing on HTML files and lookfor for particular tags.

First off given a single line that I want to find a string in, would
it be quicker to do

if string.find(line, searchString) > -1:
    #process line

or 

result = re.compile (searchString).match(line)
if result:

Now, expanding the question, which would probably be quicker.

for line in file.readlines():
   if string.find (....

or 

fileContents = file.read()
searchResults = re.compile (searchString).search(fileContents).

and then looping over searchResults

Thanks



More information about the Python-list mailing list