Issue #4677: add two list comprehension tests to pybench.
[python.git] / Tools / pybench / Lists.py
blob6c297a3416b2f1f96d4037c2b54d7acb59905b05
1 from pybench import Test
3 class SimpleListManipulation(Test):
5 version = 2.0
6 operations = 5* (6 + 6 + 6)
7 rounds = 130000
9 def test(self):
11 l = []
12 append = l.append
14 for i in xrange(self.rounds):
16 append(2)
17 append(3)
18 append(4)
19 append(2)
20 append(3)
21 append(4)
23 l[0] = 3
24 l[1] = 4
25 l[2] = 5
26 l[3] = 3
27 l[4] = 4
28 l[5] = 5
30 x = l[0]
31 x = l[1]
32 x = l[2]
33 x = l[3]
34 x = l[4]
35 x = l[5]
37 append(2)
38 append(3)
39 append(4)
40 append(2)
41 append(3)
42 append(4)
44 l[0] = 3
45 l[1] = 4
46 l[2] = 5
47 l[3] = 3
48 l[4] = 4
49 l[5] = 5
51 x = l[0]
52 x = l[1]
53 x = l[2]
54 x = l[3]
55 x = l[4]
56 x = l[5]
58 append(2)
59 append(3)
60 append(4)
61 append(2)
62 append(3)
63 append(4)
65 l[0] = 3
66 l[1] = 4
67 l[2] = 5
68 l[3] = 3
69 l[4] = 4
70 l[5] = 5
72 x = l[0]
73 x = l[1]
74 x = l[2]
75 x = l[3]
76 x = l[4]
77 x = l[5]
79 append(2)
80 append(3)
81 append(4)
82 append(2)
83 append(3)
84 append(4)
86 l[0] = 3
87 l[1] = 4
88 l[2] = 5
89 l[3] = 3
90 l[4] = 4
91 l[5] = 5
93 x = l[0]
94 x = l[1]
95 x = l[2]
96 x = l[3]
97 x = l[4]
98 x = l[5]
100 append(2)
101 append(3)
102 append(4)
103 append(2)
104 append(3)
105 append(4)
107 l[0] = 3
108 l[1] = 4
109 l[2] = 5
110 l[3] = 3
111 l[4] = 4
112 l[5] = 5
114 x = l[0]
115 x = l[1]
116 x = l[2]
117 x = l[3]
118 x = l[4]
119 x = l[5]
121 if len(l) > 10000:
122 # cut down the size
123 del l[:]
125 def calibrate(self):
127 l = []
128 append = l.append
130 for i in xrange(self.rounds):
131 pass
133 class ListSlicing(Test):
135 version = 2.0
136 operations = 25*(3+1+2+1)
137 rounds = 800
139 def test(self):
141 n = range(100)
142 r = range(25)
144 for i in xrange(self.rounds):
146 l = n[:]
148 for j in r:
150 m = l[50:]
151 m = l[:25]
152 m = l[50:55]
153 l[:3] = n
154 m = l[:-1]
155 m = l[1:]
156 l[-1:] = n
158 def calibrate(self):
160 n = range(100)
161 r = range(25)
163 for i in xrange(self.rounds):
164 for j in r:
165 pass
167 class SmallLists(Test):
169 version = 2.0
170 operations = 5*(1+ 6 + 6 + 3 + 1)
171 rounds = 80000
173 def test(self):
175 for i in xrange(self.rounds):
177 l = []
179 append = l.append
180 append(2)
181 append(3)
182 append(4)
183 append(2)
184 append(3)
185 append(4)
187 l[0] = 3
188 l[1] = 4
189 l[2] = 5
190 l[3] = 3
191 l[4] = 4
192 l[5] = 5
194 l[:3] = [1,2,3]
195 m = l[:-1]
196 m = l[1:]
198 l[-1:] = [4,5,6]
200 l = []
202 append = l.append
203 append(2)
204 append(3)
205 append(4)
206 append(2)
207 append(3)
208 append(4)
210 l[0] = 3
211 l[1] = 4
212 l[2] = 5
213 l[3] = 3
214 l[4] = 4
215 l[5] = 5
217 l[:3] = [1,2,3]
218 m = l[:-1]
219 m = l[1:]
221 l[-1:] = [4,5,6]
223 l = []
225 append = l.append
226 append(2)
227 append(3)
228 append(4)
229 append(2)
230 append(3)
231 append(4)
233 l[0] = 3
234 l[1] = 4
235 l[2] = 5
236 l[3] = 3
237 l[4] = 4
238 l[5] = 5
240 l[:3] = [1,2,3]
241 m = l[:-1]
242 m = l[1:]
244 l[-1:] = [4,5,6]
246 l = []
248 append = l.append
249 append(2)
250 append(3)
251 append(4)
252 append(2)
253 append(3)
254 append(4)
256 l[0] = 3
257 l[1] = 4
258 l[2] = 5
259 l[3] = 3
260 l[4] = 4
261 l[5] = 5
263 l[:3] = [1,2,3]
264 m = l[:-1]
265 m = l[1:]
267 l[-1:] = [4,5,6]
269 l = []
271 append = l.append
272 append(2)
273 append(3)
274 append(4)
275 append(2)
276 append(3)
277 append(4)
279 l[0] = 3
280 l[1] = 4
281 l[2] = 5
282 l[3] = 3
283 l[4] = 4
284 l[5] = 5
286 l[:3] = [1,2,3]
287 m = l[:-1]
288 m = l[1:]
290 l[-1:] = [4,5,6]
292 def calibrate(self):
294 for i in xrange(self.rounds):
295 pass
297 class SimpleListComprehensions(Test):
299 version = 2.0
300 operations = 6
301 rounds = 20000
303 def test(self):
305 n = range(10) * 10
307 for i in xrange(self.rounds):
308 l = [x for x in n]
309 l = [x for x in n if x]
310 l = [x for x in n if not x]
312 l = [x for x in n]
313 l = [x for x in n if x]
314 l = [x for x in n if not x]
316 def calibrate(self):
318 n = range(10) * 10
320 for i in xrange(self.rounds):
321 pass
323 class NestedListComprehensions(Test):
325 version = 2.0
326 operations = 6
327 rounds = 20000
329 def test(self):
331 m = range(10)
332 n = range(10)
334 for i in xrange(self.rounds):
335 l = [x for x in n for y in m]
336 l = [y for x in n for y in m]
338 l = [x for x in n for y in m if y]
339 l = [y for x in n for y in m if x]
341 l = [x for x in n for y in m if not y]
342 l = [y for x in n for y in m if not x]
344 def calibrate(self):
346 m = range(10)
347 n = range(10)
349 for i in xrange(self.rounds):
350 pass