- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How do I clear the regular expression cache in Python?
Presently, when regular expressions are compiled, the result is cached so that if the same regex is compiled again, it is retrieved from the cache and no extra effort is required. This cache supports up to 100 entries. Once the 100th entry is reached, the cache is cleared and a new compile must occur.
The objective of caching is to decrease the average call time of the function. The overhead associated with keeping more information in _cache and paring it instead of clearing it would increase that average call time. The _cache.clear() call will complete quickly, and even though cache is lost this is preferable over maintaining a cache state and having the overhead of removing individual elements from the cache when the limit is reached.
There are a few things to think about when calculating the cache efficiency −
Average call time on cache hits (very short)
Average call time on cache misses (longer)
Frequency of cache hits (fairly uncommon)
Call time when cache is cleared or pruned (fairly uncommon)
The regular expression cache is cleared using _cache.clear() when it reaches _MAXCACHE of entries.
- Related Articles
- How do I cache method calls in Python?
- How do we use re.finditer() method in Python regular expression?
- How do we use Python Regular Expression named groups?
- How do you validate a URL with a regular expression in Python?
- How can I find all matches to a regular expression in Python?
- How do we find the exact positions of each match in Python regular expression?
- Why do we use re.compile() method in Python regular expression?
- How do I clear the cin buffer in C++?
- How do I clear the usage of setInterval()?
- How do we use a delimiter to split string in Python regular expression?
- How regular expression modifiers work in Python?
- How regular expression grouping works in Python?
- How regular expression alternatives work in Python?
- How do we use Python regular expression to match a date string?