Skip to content

Instantly share code, notes, and snippets.

@andriykohut
Created August 11, 2017 09:20
Show Gist options
  • Save andriykohut/4bc94dd90364357cb69584670718743f to your computer and use it in GitHub Desktop.
Save andriykohut/4bc94dd90364357cb69584670718743f to your computer and use it in GitHub Desktop.
re.compile once vs each time
import re
GAMES_RE = re.compile('games\.buzzfeed\.com')
STRING = "https://games.buzzfeed.com/something https://games.buzzfeed.com/something https://games.buzzfeed.com/something"
def test_precompiled():
GAMES_RE.search(STRING)
def test_compile_each_time():
re.compile('games\.buzzfeed\.com').search(STRING)
if __name__ == '__main__':
import timeit
print(timeit.timeit("test_precompiled()", globals=globals())) # 0.4027057359999162
print(timeit.timeit("test_compile_each_time()", globals=globals())) # 0.9667623320001439
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment