Precompiled regular expressions slower?

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Tue Feb 26 13:50:38 EST 2002


On Tue, 26 Feb 2002 11:17:53 -0500, Peter Bienstman <pbienst at mit.edu> wrote:
>
>re1 = compile(r"....") 
>...
>line = inputfile.readline()
>while line:
>  s = search(re1, line)
>  ...
>  line = inputfile.readline()
>
>Strangely enough this new version seems to run about 30% *slower* than the 
>old version.
>
>Am I missing something here? (I'm using Python 2.1)

Yes.  Use re1.search(line) instead.  

The times of the following three tests are:
1.94322395325   # search(string, string)
2.62431204319   # search(re_obj, string)
0.667925000191  # re_obj.search(string)

Someone care to explain why the second ends up slower than the first?

Huaiyu

# =========================== test for re speed ===============================
from re import search, compile
from time import time

a = 'dsa'
b = 'adsfasf dsaf adsf a'

def test1(a, b):
    _time = time();     
    for i in range(100000):
        search(a, b)
    print time() - _time

def test2(a, b):
    a = compile(a)
    _time = time();     
    for i in range(100000):
        search(a, b)
    print time() - _time

def test3(a, b):
    a = compile(a)
    _time = time();     
    for i in range(100000):
        a.search(b)
    print time() - _time

test1(a, b)
test2(a, b)
test3(a, b)





More information about the Python-list mailing list