Skip to content

Instantly share code, notes, and snippets.

@aturley
Created June 19, 2012 18:45
Show Gist options
  • Save aturley/2955828 to your computer and use it in GitHub Desktop.
Save aturley/2955828 to your computer and use it in GitHub Desktop.
regex vs split and test in Python
aturley@silverslab [~/play]$ cat regex_vs_split.py
import time
import re
url = "http://octopart.com/api/v2/parts/match?manufacturer_name=texas+instruments&mpn=SN74LS240N&suppress_status_codes=True"
url_x = "http://octopart.com/api/v2/parts/match?manufacturer_name=texas+instruments&mpn=SN74LS240N"
url_l = "http://octopart.com/api/v2/parts/match?manufacturer_name=texas+instruments&mpn=SN74LS240N&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&suppress_status_codes=True"
suppress_status_codes_regex = re.compile(
'(^|&)suppress_status_codes=(1|true|True)')
def use_split(url):
return next((_ for _ in url.split('&') if _ in ["supress_status_codes=1",
"supress_status_codes=True",
"supress_status_codes=true"]), False)
def use_regex(url):
return suppress_status_codes_regex.search(url)
def main():
for f in [use_split, use_regex]:
for u in [url, url_x, url_l]:
start = time.time()
for i in xrange(0, 100000):
f(u)
end = time.time()
print "%s took %f seconds for %s"%(f.func_name, end - start, u)
if __name__ == "__main__":
main()
aturley@silverslab [~/play]$ python regex_vs_split.py
use_split took 0.346677 seconds for http://octopart.com/api/v2/parts/match?manufacturer_name=texas+instruments&mpn=SN74LS240N&suppress_status_codes=True
use_split took 0.304294 seconds for http://octopart.com/api/v2/parts/match?manufacturer_name=texas+instruments&mpn=SN74LS240N
use_split took 5.151054 seconds for http://octopart.com/api/v2/parts/match?manufacturer_name=texas+instruments&mpn=SN74LS240N&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&suppress_status_codes=True
use_regex took 0.864007 seconds for http://octopart.com/api/v2/parts/match?manufacturer_name=texas+instruments&mpn=SN74LS240N&suppress_status_codes=True
use_regex took 0.627309 seconds for http://octopart.com/api/v2/parts/match?manufacturer_name=texas+instruments&mpn=SN74LS240N
use_regex took 8.329847 seconds for http://octopart.com/api/v2/parts/match?manufacturer_name=texas+instruments&mpn=SN74LS240N&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&junk=1&suppress_status_codes=True
aturley@silverslab [~/play]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment