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:
replace_builtin()
~ 0.008 secreplace_re()
~ 0.035 sec (nearly 4.5 times slower!!!)
Why is that?