Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: why regular expression is slower than replace() method?

Tags:

python

regex

I have next 2 blocks of code:

def replace_re(text):
    start = time.time()
    new_text = re.compile(r'(\n|\s{4})').sub('', text)
    finish = time.time()
    return finish - start

def replace_builtin(text):
    start = time.time()
    new_text = text.replace('\n', '').replace('    ', '')
    finish = time.time()
    return finish - start

Than I call both functions with text param (~500kb of source code of one web-page). I thought replace_re() will be much faster, but results are the next:

  1. replace_builtin() ~ 0.008 sec
  2. replace_re() ~ 0.035 sec (nearly 4.5 times slower!!!)

Why is that?

like image 326
Vitalii Ponomar Avatar asked Sep 18 '26 16:09

Vitalii Ponomar


2 Answers

Because regular expressions are more than 4.5 times more complex than a fixed string replacement.

like image 157
lanzz Avatar answered Sep 21 '26 04:09

lanzz


Because an re has to generate a FSM. Then use that to process the string. While a replace can use the underlying string processing functions closer to the lib/OS levels.

like image 33
Jon Clements Avatar answered Sep 21 '26 05:09

Jon Clements



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!