Which is faster...find or re. ?

Keith Dart kdart at kdart.com
Sat Aug 3 05:49:50 EDT 2002


In article <7d3dc526.0208020948.15403600 at posting.google.com>, "Fearless
Freep" <joconnor at cybermesa.com> penned these words:

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

I did some testing of this a little while ago. The answer is, as usual:
"It depends". If you are doing a simple string match (or find) then
string.find is about ten times faster than a RE. It is almost alwasy
faster. However, if you are doing some kind of parsing, REs are faster
that scanning/chopping/slicking strings in python. The more complex the
string matching and (especially) cutting out bits of strings then REs are
faster. 

HTH,
Keith
 

-- 
--                           ^
                           \/ \/
                           (O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart
<mailto:kdart at kdart.com> 
<http://www.kdart.com/>  
----------------------------------------------------------------------------
Public key ID: B08B9D2C Public key: <http://www.kdart.com/~kdart/public.key>
============================================================================



More information about the Python-list mailing list