I'm working through Doug Hellman's "The Python Standard Library by Example" and came across this:
"1.3.2 Compiling Expressions
re includes module-level functions for working with regular expressions as text strings, but it is more efficient to compile the expressions a program uses frequently."
I couldn't follow his explanation for why this is the case.  He says that the "module-level functions maintain a cache of compiled expressions" and that since the "size of the cache" is limited, "using compiled expressions directly avoids the cache lookup overhead."
I'd greatly appreciate it if someone could please explain or direct me to an explanation that I could better understand for why it is more efficient to compile the regular expressions a program uses frequently, and how this process actually works.
It sounds to me like the author is simply saying it's more efficient to compile a regex and save that than to count on a previously compiled version of it still being held in the module's limited-size internal cache. This is probably because to the amount of effort it takes to compile them plus the extra cache lookup overhead that must first occur being greater than the client simply storing them itself.
Compiling Regular Expressions in Python