OK to memoize re objects?

Hyuga hyugaricdeau at gmail.com
Tue Sep 22 16:46:25 CEST 2009

On Sep 21, 11:02 am, Nobody <nob... at nowhere.com> wrote:
> On Mon, 21 Sep 2009 07:11:36 -0700, Ethan Furman wrote:
> > Looking in the code for re in 2.5:
> > _MAXCACHE = 100
> > On the other hand, I (a
> > re novice, to be sure) have only used between two to five in any one
> > program... it'll be a while before I hit _MAXCACHE!
> Do you know how many REs import-ed modules are using? The cache isn't
> reserved for __main__.

Based on this, I'd say that the best policy would be that if you only
have a handful of simple REs that are used only on occasion, it's
probably not worth using re.compile--even if they fall out of cache,
it shouldn't take a noticeable amount of time to recompile them.

If, however, these are either complex REs, or REs that are being used
very frequently, say in a loop, might as well save the compiled RE
somewhere just to be sure it doesn't have to be recompiled at any

More information about the Python-list mailing list