Is 1 large Regex faster than 3 smaller ones?

Roy Smith roy at panix.com
Sun Jun 3 23:28:33 CEST 2007


In article <1180896727.419817.303960 at p47g2000hsd.googlegroups.com>,
 erikcw <erikwickstrom at gmail.com> wrote:

> Hi,
> 
> I need to match 3 small strings in a small text file (about 200 words
> of text).
> 
> Would it be faster to write 1 compiled regex that matches all 3
> substrings in one go, or to use 3 separate regular expressions to do
> the same job?
> 
> Thanks!
> Erik

For a classic regex, the answer is one big one.  Matching against a regex 
takes time proportional to the number of characters of input.  One big 
regex will probably consume more memory, and may be slower to compile, but 
it should run faster.

On the other hand, there are a lot of things that pattern maching libraries 
accept these days under the guise of being a "regex" which are not strictly 
regexes in the classic sense.

>From a purely practical point of view, if your input is only 200 words, 
it's likely that the search time will be insignificant no matter what you 
do.



More information about the Python-list mailing list