/Tools/pybench/Lists.py

http://unladen-swallow.googlecode.com/ · Python · 350 lines · 264 code · 85 blank · 1 comment · 39 complexity · d411c565703977dfb74397434368fac3 MD5 · raw file

  1. from pybench import Test
  2. class SimpleListManipulation(Test):
  3. version = 2.0
  4. operations = 5* (6 + 6 + 6)
  5. rounds = 130000
  6. def test(self):
  7. l = []
  8. append = l.append
  9. for i in xrange(self.rounds):
  10. append(2)
  11. append(3)
  12. append(4)
  13. append(2)
  14. append(3)
  15. append(4)
  16. l[0] = 3
  17. l[1] = 4
  18. l[2] = 5
  19. l[3] = 3
  20. l[4] = 4
  21. l[5] = 5
  22. x = l[0]
  23. x = l[1]
  24. x = l[2]
  25. x = l[3]
  26. x = l[4]
  27. x = l[5]
  28. append(2)
  29. append(3)
  30. append(4)
  31. append(2)
  32. append(3)
  33. append(4)
  34. l[0] = 3
  35. l[1] = 4
  36. l[2] = 5
  37. l[3] = 3
  38. l[4] = 4
  39. l[5] = 5
  40. x = l[0]
  41. x = l[1]
  42. x = l[2]
  43. x = l[3]
  44. x = l[4]
  45. x = l[5]
  46. append(2)
  47. append(3)
  48. append(4)
  49. append(2)
  50. append(3)
  51. append(4)
  52. l[0] = 3
  53. l[1] = 4
  54. l[2] = 5
  55. l[3] = 3
  56. l[4] = 4
  57. l[5] = 5
  58. x = l[0]
  59. x = l[1]
  60. x = l[2]
  61. x = l[3]
  62. x = l[4]
  63. x = l[5]
  64. append(2)
  65. append(3)
  66. append(4)
  67. append(2)
  68. append(3)
  69. append(4)
  70. l[0] = 3
  71. l[1] = 4
  72. l[2] = 5
  73. l[3] = 3
  74. l[4] = 4
  75. l[5] = 5
  76. x = l[0]
  77. x = l[1]
  78. x = l[2]
  79. x = l[3]
  80. x = l[4]
  81. x = l[5]
  82. append(2)
  83. append(3)
  84. append(4)
  85. append(2)
  86. append(3)
  87. append(4)
  88. l[0] = 3
  89. l[1] = 4
  90. l[2] = 5
  91. l[3] = 3
  92. l[4] = 4
  93. l[5] = 5
  94. x = l[0]
  95. x = l[1]
  96. x = l[2]
  97. x = l[3]
  98. x = l[4]
  99. x = l[5]
  100. if len(l) > 10000:
  101. # cut down the size
  102. del l[:]
  103. def calibrate(self):
  104. l = []
  105. append = l.append
  106. for i in xrange(self.rounds):
  107. pass
  108. class ListSlicing(Test):
  109. version = 2.0
  110. operations = 25*(3+1+2+1)
  111. rounds = 800
  112. def test(self):
  113. n = range(100)
  114. r = range(25)
  115. for i in xrange(self.rounds):
  116. l = n[:]
  117. for j in r:
  118. m = l[50:]
  119. m = l[:25]
  120. m = l[50:55]
  121. l[:3] = n
  122. m = l[:-1]
  123. m = l[1:]
  124. l[-1:] = n
  125. def calibrate(self):
  126. n = range(100)
  127. r = range(25)
  128. for i in xrange(self.rounds):
  129. for j in r:
  130. pass
  131. class SmallLists(Test):
  132. version = 2.0
  133. operations = 5*(1+ 6 + 6 + 3 + 1)
  134. rounds = 80000
  135. def test(self):
  136. for i in xrange(self.rounds):
  137. l = []
  138. append = l.append
  139. append(2)
  140. append(3)
  141. append(4)
  142. append(2)
  143. append(3)
  144. append(4)
  145. l[0] = 3
  146. l[1] = 4
  147. l[2] = 5
  148. l[3] = 3
  149. l[4] = 4
  150. l[5] = 5
  151. l[:3] = [1,2,3]
  152. m = l[:-1]
  153. m = l[1:]
  154. l[-1:] = [4,5,6]
  155. l = []
  156. append = l.append
  157. append(2)
  158. append(3)
  159. append(4)
  160. append(2)
  161. append(3)
  162. append(4)
  163. l[0] = 3
  164. l[1] = 4
  165. l[2] = 5
  166. l[3] = 3
  167. l[4] = 4
  168. l[5] = 5
  169. l[:3] = [1,2,3]
  170. m = l[:-1]
  171. m = l[1:]
  172. l[-1:] = [4,5,6]
  173. l = []
  174. append = l.append
  175. append(2)
  176. append(3)
  177. append(4)
  178. append(2)
  179. append(3)
  180. append(4)
  181. l[0] = 3
  182. l[1] = 4
  183. l[2] = 5
  184. l[3] = 3
  185. l[4] = 4
  186. l[5] = 5
  187. l[:3] = [1,2,3]
  188. m = l[:-1]
  189. m = l[1:]
  190. l[-1:] = [4,5,6]
  191. l = []
  192. append = l.append
  193. append(2)
  194. append(3)
  195. append(4)
  196. append(2)
  197. append(3)
  198. append(4)
  199. l[0] = 3
  200. l[1] = 4
  201. l[2] = 5
  202. l[3] = 3
  203. l[4] = 4
  204. l[5] = 5
  205. l[:3] = [1,2,3]
  206. m = l[:-1]
  207. m = l[1:]
  208. l[-1:] = [4,5,6]
  209. l = []
  210. append = l.append
  211. append(2)
  212. append(3)
  213. append(4)
  214. append(2)
  215. append(3)
  216. append(4)
  217. l[0] = 3
  218. l[1] = 4
  219. l[2] = 5
  220. l[3] = 3
  221. l[4] = 4
  222. l[5] = 5
  223. l[:3] = [1,2,3]
  224. m = l[:-1]
  225. m = l[1:]
  226. l[-1:] = [4,5,6]
  227. def calibrate(self):
  228. for i in xrange(self.rounds):
  229. pass
  230. class SimpleListComprehensions(Test):
  231. version = 2.0
  232. operations = 6
  233. rounds = 20000
  234. def test(self):
  235. n = range(10) * 10
  236. for i in xrange(self.rounds):
  237. l = [x for x in n]
  238. l = [x for x in n if x]
  239. l = [x for x in n if not x]
  240. l = [x for x in n]
  241. l = [x for x in n if x]
  242. l = [x for x in n if not x]
  243. def calibrate(self):
  244. n = range(10) * 10
  245. for i in xrange(self.rounds):
  246. pass
  247. class NestedListComprehensions(Test):
  248. version = 2.0
  249. operations = 6
  250. rounds = 20000
  251. def test(self):
  252. m = range(10)
  253. n = range(10)
  254. for i in xrange(self.rounds):
  255. l = [x for x in n for y in m]
  256. l = [y for x in n for y in m]
  257. l = [x for x in n for y in m if y]
  258. l = [y for x in n for y in m if x]
  259. l = [x for x in n for y in m if not y]
  260. l = [y for x in n for y in m if not x]
  261. def calibrate(self):
  262. m = range(10)
  263. n = range(10)
  264. for i in xrange(self.rounds):
  265. pass