Skip to content

Instantly share code, notes, and snippets.

@cmccandless
Last active August 6, 2020 17:46
Show Gist options
  • Save cmccandless/63a3560c42fa54014a67921d1db9fea3 to your computer and use it in GitHub Desktop.
Save cmccandless/63a3560c42fa54014a67921d1db9fea3 to your computer and use it in GitHub Desktop.
Python regex compiled vs inline
import re
from pathlib import Path
p = Path("big.txt")
t = p.read_text()
rgx = re.compile(r"\w+")
for _ in range(10):
rgx.findall(t)
import re
from pathlib import Path
p = Path("big.txt")
t = p.read_text()
rgx = re.compile(r"\w+")
for _ in range(10):
rgx.findall(t)
import re
from pathlib import Path
p = Path("big.txt")
t = p.read_text()
for _ in range(10):
re.findall(r'\w+', t)
#!/bin/bash
# big.txt from https://norvig.com/big.txt
time python3.8 compiled.py
# real 0m2.292s
# user 0m2.091s
# sys 0m0.200s
time python3.8 literal.py
#
# real 0m2.287s
# user 0m2.030s
# sys 0m0.257s
time python3.8 compiled_in_loop.py
#
# real 0m2.286s
# user 0m2.025s
# sys 0m0.260s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment