【发布时间】:2019-04-14 22:11:37
【问题描述】:
我注意到通过编译模式进行预处理会加快匹配操作,就像下面的例子一样。
python3 -m timeit -s "import re; t = re.compile(r'[\w+][\d]+')" "t.findall('abc eft123&aaa123')"
1000000 loops, best of 3: 1.42 usec per loop
python3 -m timeit -s "import re;" "re.findall(r'[\w+][\d]+', 'abc eft123&aaa123')"
100000 loops, best of 3: 2.45 usec per loop
但是如果我改变编译模式和重新模块的顺序,结果就不一样了,现在似乎慢了很多,为什么会这样?
python3 -m timeit -s "import re; t = re.compile(r'[\w+][\d]+')" "re.findall(t, 'abc eft123&aaa123')"
100000 loops, best of 3: 3.66 usec per loop
【问题讨论】:
标签: python regex compilation findall