[Python-Dev] Speeding up regular expression compilation

Bram Moolenaar Bram at moolenaar.net
Fri Oct 31 06:20:44 EST 2003

Barry Warsaw wrote:

> On Thu, 2003-10-30 at 14:40, Skip Montanaro wrote:
> > Not really.  Note though that the sre module caches compiled regular
> > expressions.  How many it caches depends on the size of sre._MAXCACHE
> > (default is 100).  If you have many more regular expressions than that,
> > you'll spend a lot of time compiling them.  You might find it helpful to
> > boost that number.
> Of course you can just assign your compiled regular expression objects
> to a global or local and use that.  Instant caching!  Which is what I
> tend to do.

I'm already caching all the compiled patterns.  It's the first-time
compile that is consuming time, there are a lot of patterns.  But half a
second to compile them is too much, the whole program may not run longer
than a second.

BTW. I've changed the code to use pre.py on Python 2.3 (with the warning
removed) as a temporary solution.  The problem will be back with 2.4...

The reason I sent this to the development list is that I thought this
could be solved on the library side.  Changing the Python code sounds
like working around the real problem.

BRIDGEKEEPER: What is your favorite colour?
GAWAIN:       Blue ...  No yelloooooww!
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

 /// Bram Moolenaar -- Bram at Moolenaar.net -- http://www.Moolenaar.net   \\\
///          Creator of Vim - Vi IMproved -- http://www.Vim.org          \\\
\\\              Project leader for A-A-P -- http://www.A-A-P.org        ///
 \\\  Help AIDS victims, buy here: http://ICCF-Holland.org/click1.html  ///

More information about the Python-Dev mailing list