やりたいこと
Google の正規表現ライブラリの re2 の実行時間を調べる。
→ 調べてみると、re に比べて超高速だった。
re2
re2 のインストール
$ pip install google-re2
re2 の使い方
re2 は re と同じ compile() や search() などのメソッドが使用できる。
>>> import re2
>>> pttrn = re2.compile('def')
>>> pttrn.search('abcdefghi')
<re2._Match object at 0x7f85b74ffe20>
cProfile による re2 の実行時間の計測
プログラム
test_re2.py
import re
import regex
import re2
import cProfile, pstats, io
def compile_re(pttrn_str):
pttrn = re.compile(pttrn_str)
return pttrn
def compile_regex(pttrn_str):
pttrn = regex.compile(pttrn_str)
return pttrn
def compile_re2(pttrn_str):
pttrn = re2.compile(pttrn_str)
return pttrn
def search_re(text, pttrn):
m = pttrn.search(text)
return m
def search_regex(text, pttrn):
m = pttrn.search(text)
return m
def search_re2(text, pttrn):
m = pttrn.search(text)
return m
def test(num):
text = 'a' * 1000 + '0' * 1000
for i in range(0, num):
pttrn_str = 'a' * 50 + f'{i}'
pttrn_re = compile_re(pttrn_str)
search_re(text, pttrn_re)
pttrn_regex = compile_regex(pttrn_str)
search_regex(text, pttrn_regex)
pttrn_re2 = compile_re2(pttrn_str)
search_re2(text, pttrn_re2)
def main():
test(10000)
return 0
if __name__ == '__main__':
pr = cProfile.Profile()
pr.enable()
# 計測したい処理
res = main()
pr.disable
# 出力
s = io.StringIO()
ps = pstats.Stats(pr, stream=s).sort_stats("cumulative") or "tottime"
ps.print_stats(1000) # 上位1000件
print(s.getvalue())
exit(res)
実行結果
実行結果は以下の通り。※見やすさのため必要な行のみを行を入れ替えている。
$ python test_re2.py
21295701 function calls (21285670 primitive calls) in 10.432 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
10000 0.008 0.000 6.904 0.001 /home/n-ikeda/work/python/regex/test_re2.py:17(compile_regex)
10000 0.007 0.000 2.460 0.000 /home/n-ikeda/work/python/regex/test_re2.py:12(compile_re)
1 0.000 0.000 0.001 0.001 /home/n-ikeda/work/python/regex/test_re2.py:22(compile_re2)
10000 0.006 0.000 0.057 0.000 /home/n-ikeda/work/python/regex/test_re2.py:32(search_regex)
10000 0.006 0.000 0.049 0.000 /home/n-ikeda/work/python/regex/test_re2.py:27(search_re)
1 0.000 0.000 0.002 0.002 /home/n-ikeda/work/python/regex/test_re2.py:37(search_re2)
コンパイル、マッチングの両方で re2 が圧倒的に実行時間(cumtime)が短いことがわかる。
なお、compile_re2、search_re2 で実行回数(ncalls)が1となっているのは、cProfile では re2 内の C/C++ 関数が呼び出されたかを追跡しないためだが、tottime/cumtime には時間が反映されている。