[Python-Dev] Revising RE docs

Gareth McCaughan gmccaughan at synaptics-uk.com
Fri Sep 2 12:40:35 CEST 2005

On Thursday 2005-09-01 18:09, Guido van Rossum wrote:

> They *are* cached and there is no cost to using the functions instead
> of the methods unless you have so many regexps in your program that
> the cache is cleared (the limit is 100).

Sure there is; the cost of looking them up in the cache.

    >>> import re,timeit

    >>> timeit.re=re
    >>> timeit.Timer("""re.search(r"(\d*).*(\d*)", "abc123def456")""").timeit(1000000)

    >>> timeit.r = re.compile(r"(\d*).*(\d*)")
    >>> timeit.Timer("""r.search("abc123def456")""").timeit(1000000)

    >>> timeit.Timer().timeit(1000000)

So in this (highly artificial toy) application it's about 7.5/2.5 = 3 times
faster to use the methods instead of the functions.


More information about the Python-Dev mailing list