-
-
Save MostAwesomeDude/754873 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 | |
"""The results from my computer: | |
Good headers: | |
List creation and iteration: 4.34622311592 | |
List iteration and substitution: 3.61615085602 | |
LC (spendy): 9.65025401115 | |
Mixed headers: | |
List creation and iteration: 8.39401197433 | |
List iteration and substitution: 3.59153795242 | |
LC (spendy): 10.9298632145 | |
Bad headers: | |
List creation and iteration: 8.26548790932 | |
List iteration and substitution: 3.48538517952 | |
LC (spendy): 9.16864705086 | |
""" | |
headers_good = [ | |
('Content-Type', "text/plain"), | |
('Content-Length', '27'), | |
('Content-MD5', "1234567890123456789012") | |
] | |
headers_mixed = [ | |
('Content-Type', u"text/plain"), | |
('Content-Length', '27'), | |
(u'Content-MD5', u"1234567890123456789012") | |
] | |
headers_bad = [ | |
(u'Content-Type', u"text/plain"), | |
(u'Content-Length', u'27'), | |
(u'Content-MD5', u"1234567890123456789012") | |
] | |
def foo(headers): | |
headers_ = list() | |
for name, value in headers: | |
if isinstance(name, unicode): | |
name = name.encode('iso-8859-1') | |
value = value.encode('iso-8859-1') | |
headers_.append((name, value)) | |
return headers_ | |
def bar(headers): | |
for i in xrange(len(headers)): | |
name, value = headers[i] | |
if not isinstance(name, unicode) and not isinstance(value, unicode): | |
continue | |
if isinstance(name, unicode): | |
name = name.encode('iso-8859-1') | |
if isinstance(value, unicode): | |
value = value.encode('iso-8859-1') | |
headers[i] = (name, value) | |
return headers | |
def baz(headers): | |
return [(unicode(k).encode("iso-8859-1"), unicode(v).encode("iso-8859-1")) | |
for k, v in headers] | |
if __name__ == '__main__': | |
from timeit import Timer | |
print "Good headers:" | |
t = Timer("foo(headers_good)", "from __main__ import foo, headers_good") | |
print "List creation and iteration:", t.timeit() | |
t = Timer("bar(headers_good)", "from __main__ import bar, headers_good") | |
print "List iteration and substitution:", t.timeit() | |
t = Timer("baz(headers_good)", "from __main__ import baz, headers_good") | |
print "LC (spendy):", t.timeit() | |
print "\nMixed headers:" | |
t = Timer("foo(headers_mixed)", "from __main__ import foo, headers_mixed") | |
print "List creation and iteration:", t.timeit() | |
t = Timer("bar(headers_mixed)", "from __main__ import bar, headers_mixed") | |
print "List iteration and substitution:", t.timeit() | |
t = Timer("baz(headers_mixed)", "from __main__ import baz, headers_mixed") | |
print "LC (spendy):", t.timeit() | |
print "\nBad headers:" | |
t = Timer("foo(headers_bad)", "from __main__ import foo, headers_bad") | |
print "List creation and iteration:", t.timeit() | |
t = Timer("bar(headers_bad)", "from __main__ import bar, headers_bad") | |
print "List iteration and substitution:", t.timeit() | |
t = Timer("baz(headers_bad)", "from __main__ import baz, headers_bad") | |
print "LC (spendy):", t.timeit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I see your optimization of new list creation, but if one element of the 2-tuple is unicode, the other might not be, and there is no bytes.encode method on py3k. (A requirement for my project.) I've updated my copy of the PEP to be more fair to list creation and more soundly test (averaging results).