I have two questions about python re module.
I updated my application of python version to 3.7. After I did so, I noticed a slowdown in the re.search().
I did simple test, it may be that the average time has increased since Python 3.7.
In my app, the regular expression pattern is different every time. Therefore, there is no need to use re.compile().
- simple test
#!/usr/bin/env python
import re
import timeit
def func():
for _ in range(100):
re.search(r'\d+', 'hoge123fuga')
if __name__ == '__main__':
max_time = 0
min_time = 1e9
results = []
for _ in range(10):
result = timeit.timeit(lambda: func, number=10000)
if result < min_time:
min_time = result
if max_time < result:
max_time = result
results.append(result)
average_time = float(sum(results)) / float(10)
print('max_time: {}'.format(max_time))
print('average_time: {}'.format(average_time))
print('min_time: {}'.format(min_time))
- simple test results
python_version, average_time
2.7.17, 0.721276259422
3.5.9, 0.7110545930016088
3.6.10, 0.7553750206978294
3.7.6, 0.8779164923000001
3.8.1, 0.7880382750000001
So I have two questions.
- Is re.search () slower since Python 3.7? Why is this?
- Is it possible to make
re.search()of Python3.7 as fast as Python 2.x?
Thank you for reading my question.
- My environment
- macOS Catalina
- Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
- 16 GB 2133 MHz LPDDR3