Created
August 11, 2017 09:20
-
-
Save andriykohut/4bc94dd90364357cb69584670718743f to your computer and use it in GitHub Desktop.
re.compile once vs each time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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