In [1]: import re
In [2]: %timeit re.match(r'\d*', '1'*1000000)
12.5 ms ± 112 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [3]: %timeit re.match(r'\d{0,}', '1'*1000000)
49.9 µs ± 200 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [4]: %timeit re.match(r'\d+', '1'*1000000)
12.5 ms ± 103 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [5]: %timeit re.match(r'\d{1,}', '1'*1000000)
49.7 µs ± 1.29 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [6]: %timeit re.match(r'\d?', '1'*1000000)
50.3 µs ± 1.75 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [7]: %timeit re.match(r'\d{0,1}', '1'*1000000)
49.8 µs ± 417 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
すべて*
、+
、?
記法よりも{n,m}
記法の方が速い。
以下、Regular Expression はもっと Expression を Regular にすべきという方向で話を進めていく予定です。