正規表現 is ~2.33× slower than startswith
For a fixed prefix, `startswith` is ~2.3× faster than a compiled regex.
Result
| Run | startswith | 正規表現 | Ratio |
|---|---|---|---|
| Run 1 | 23.523 | 57.276 | 0.41× |
| Run 2 | 22.853 | 54.715 | 0.42× |
| Run 3 | 24.042 | 54.042 | 0.44× |
Spread over 3 runs: startswith CV 2.1% / 正規表現 CV 2.5% / Ratio CV 3.5%
Conditions
200,000 words tested for the prefix `seg`; the regex is pre-compiled.
Where this number does not apply
If the pattern is dynamic or complex, a regex is the only option and this comparison does not apply.
Reproduce
cd bench && python micro_py.py --out ../data/run.json --samples 21