Skip to content

Instantly share code, notes, and snippets.

@tkaemming
Created May 12, 2010 19:54
Show Gist options
  • Save tkaemming/399058 to your computer and use it in GitHub Desktop.
Save tkaemming/399058 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import timeit
params = {
'domain': 'example.com',
'host': 'www.example.com:8000'
}
string_test = """\
import string
domain = '%(domain)s'
host = '%(host)s'
position = string.find(host, '.%%s:' %% domain)
if position > -1:
subdomain = host[:position]
""" % params
regex_test = """\
import re
domain = '%(domain)s'
host = '%(host)s'
pattern = r'^(?:(?P<subdomain>.*?)\.)?%%s(?::.*)?$' %% re.escape(domain)
matches = re.match(pattern, host)
if matches:
subdomain = matches.group('subdomain')
""" % params
print "Testing:\n", string_test
string_timer = timeit.Timer(string_test)
print string_timer.timeit()
print "Testing:\n", regex_test
regex_time = timeit.Timer(regex_test)
print regex_time.timeit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment