Guest User

Untitled

a guest
Jul 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import unicode_literals, print_function
  4.  
  5. """Compare two algorithms that canonicalize a list of tuples.
  6.  
  7. Python 2.7.0:
  8.  
  9. Good headers:
  10. List creation and iteration: 2.98474693298
  11. List iteration and substitution: 3.3568974336
  12. 11.0861445122% penalty
  13.  
  14. Mixed headers:
  15. List creation and iteration: 7.56983908017
  16. List iteration and substitution: 3.39938569069
  17. 55.0930256947% improvement
  18.  
  19. Bad headers:
  20. List creation and iteration: 10.04327027
  21. List iteration and substitution: 3.36879269282
  22. 66.4572136142% improvement
  23.  
  24.  
  25. Python 3.1.3:
  26.  
  27. Good headers:
  28. List creation and iteration: 3.96421766281
  29. List iteration and substitution: 4.36530995369
  30. 9.18817438242% penalty
  31.  
  32. Mixed headers:
  33. List creation and iteration: 11.3370667299
  34. List iteration and substitution: 4.5164826711
  35. 60.1618057058% improvement
  36.  
  37. Bad headers:
  38. List creation and iteration: 16.6681366762
  39. List iteration and substitution: 4.34285736084
  40. 73.9451538872% improvement
  41.  
  42.  
  43. """
  44.  
  45.  
  46. import sys
  47.  
  48. # Python 3.x compatibility.
  49. if sys.version_info >= (3, 0):
  50. unicode = str
  51.  
  52. else:
  53. range = xrange
  54.  
  55.  
  56.  
  57. headers_good = [
  58. (b'Content-Type', b"text/plain"),
  59. (b'Content-Length', b'27'),
  60. (b'Content-MD5', b"1234567890123456789012")
  61. ]
  62.  
  63. headers_mixed = [
  64. (b'Content-Type', "text/plain"),
  65. (b'Content-Length', b'27'),
  66. ('Content-MD5', "1234567890123456789012")
  67. ]
  68.  
  69. headers_bad = [
  70. ('Content-Type', "text/plain"),
  71. ('Content-Length', '27'),
  72. ('Content-MD5', "1234567890123456789012")
  73. ]
  74.  
  75.  
  76. def foo(headers):
  77. headers_ = list()
  78. for name, value in headers:
  79. if not isinstance(name, unicode) and not isinstance(value, unicode):
  80. continue
  81.  
  82. if isinstance(name, unicode):
  83. name = name.encode('iso-8859-1')
  84.  
  85. if isinstance(value, unicode):
  86. value = value.encode('iso-8859-1')
  87.  
  88. headers_.append((name, value))
  89. return headers_
  90.  
  91.  
  92. def bar(headers):
  93. for i in range(len(headers)):
  94. name, value = headers[i]
  95.  
  96. if not isinstance(name, unicode) and not isinstance(value, unicode):
  97. continue
  98.  
  99. if isinstance(name, unicode):
  100. name = name.encode('iso-8859-1')
  101.  
  102. if isinstance(value, unicode):
  103. value = value.encode('iso-8859-1')
  104.  
  105. headers[i] = (name, value)
  106.  
  107. return headers
  108.  
  109.  
  110. def avgresult(c, count=3.0):
  111. return sum([c() for i in range(3)]) / count
  112.  
  113.  
  114. def result(t1, t2):
  115. print(abs(1.0 - (t1 if t2 > t1 else t2) / (t2 if t2 > t1 else t1)) * 100, '%', " improvement" if 1.0 - t1 / t2 < 0 else " penalty", sep='')
  116.  
  117.  
  118. if __name__ == '__main__':
  119. from timeit import Timer
  120.  
  121. print("Running each test three times and averaging the result;\nplease wait, this could take some time.\n\nGood headers:")
  122.  
  123. t1 = avgresult(lambda: Timer("foo(headers_good)", "from __main__ import foo, headers_good").timeit())
  124. print("List creation and iteration:", t1)
  125.  
  126. t2 = avgresult(lambda: Timer("bar(headers_good)", "from __main__ import bar, headers_good").timeit())
  127. print("List iteration and substitution:", t2)
  128.  
  129. result(t1, t2)
  130.  
  131. print("\nMixed headers:")
  132.  
  133. t1 = avgresult(lambda: Timer("foo(headers_mixed)", "from __main__ import foo, headers_mixed").timeit())
  134. print("List creation and iteration:", t1)
  135.  
  136. t2 = avgresult(lambda: Timer("bar(headers_mixed)", "from __main__ import bar, headers_mixed").timeit())
  137. print("List iteration and substitution:", t2)
  138.  
  139. result(t1, t2)
  140.  
  141. print("\nBad headers:")
  142.  
  143. t1 = avgresult(lambda: Timer("foo(headers_bad)", "from __main__ import foo, headers_bad").timeit())
  144. print("List creation and iteration:", t1)
  145.  
  146. t2 = avgresult(lambda: Timer("bar(headers_bad)", "from __main__ import bar, headers_bad").timeit())
  147. print("List iteration and substitution:", t2)
  148.  
  149. result(t1, t2)
Add Comment
Please, Sign In to add comment