Skip to content

Instantly share code, notes, and snippets.

@MostAwesomeDude
Forked from amcgregor/conversion-benchmark.py
Created December 25, 2010 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MostAwesomeDude/754873 to your computer and use it in GitHub Desktop.
Save MostAwesomeDude/754873 to your computer and use it in GitHub Desktop.
#!/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()
@amcgregor
Copy link

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment