22
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【メモ】量指定子を変えると正規表現の実行速度が桁で変わる(仮)

Last updated at Posted at 2017-06-19
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 にすべきという方向で話を進めていく予定です。

22
13
1

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
22
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?