0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python の re2 による正規表現処理が超高速だった

Posted at

やりたいこと

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 には時間が反映されている。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?