Created
May 12, 2010 19:54
-
-
Save tkaemming/399058 to your computer and use it in GitHub Desktop.
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
#!/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