1from pybench import Test
2
3class DictCreation(Test):
4
5    version = 2.0
6    operations = 5*(5 + 5)
7    rounds = 80000
8
9    def test(self):
10
11        for i in xrange(self.rounds):
12
13            d1 = {}
14            d2 = {}
15            d3 = {}
16            d4 = {}
17            d5 = {}
18
19            d1 = {1:2,3:4,5:6}
20            d2 = {2:3,4:5,6:7}
21            d3 = {3:4,5:6,7:8}
22            d4 = {4:5,6:7,8:9}
23            d5 = {6:7,8:9,10:11}
24
25            d1 = {}
26            d2 = {}
27            d3 = {}
28            d4 = {}
29            d5 = {}
30
31            d1 = {1:2,3:4,5:6}
32            d2 = {2:3,4:5,6:7}
33            d3 = {3:4,5:6,7:8}
34            d4 = {4:5,6:7,8:9}
35            d5 = {6:7,8:9,10:11}
36
37            d1 = {}
38            d2 = {}
39            d3 = {}
40            d4 = {}
41            d5 = {}
42
43            d1 = {1:2,3:4,5:6}
44            d2 = {2:3,4:5,6:7}
45            d3 = {3:4,5:6,7:8}
46            d4 = {4:5,6:7,8:9}
47            d5 = {6:7,8:9,10:11}
48
49            d1 = {}
50            d2 = {}
51            d3 = {}
52            d4 = {}
53            d5 = {}
54
55            d1 = {1:2,3:4,5:6}
56            d2 = {2:3,4:5,6:7}
57            d3 = {3:4,5:6,7:8}
58            d4 = {4:5,6:7,8:9}
59            d5 = {6:7,8:9,10:11}
60
61            d1 = {}
62            d2 = {}
63            d3 = {}
64            d4 = {}
65            d5 = {}
66
67            d1 = {1:2,3:4,5:6}
68            d2 = {2:3,4:5,6:7}
69            d3 = {3:4,5:6,7:8}
70            d4 = {4:5,6:7,8:9}
71            d5 = {6:7,8:9,10:11}
72
73    def calibrate(self):
74
75        for i in xrange(self.rounds):
76            pass
77
78class DictWithStringKeys(Test):
79
80    version = 2.0
81    operations = 5*(6 + 6)
82    rounds = 200000
83
84    def test(self):
85
86        d = {}
87
88        for i in xrange(self.rounds):
89
90            d['abc'] = 1
91            d['def'] = 2
92            d['ghi'] = 3
93            d['jkl'] = 4
94            d['mno'] = 5
95            d['pqr'] = 6
96
97            d['abc']
98            d['def']
99            d['ghi']
100            d['jkl']
101            d['mno']
102            d['pqr']
103
104            d['abc'] = 1
105            d['def'] = 2
106            d['ghi'] = 3
107            d['jkl'] = 4
108            d['mno'] = 5
109            d['pqr'] = 6
110
111            d['abc']
112            d['def']
113            d['ghi']
114            d['jkl']
115            d['mno']
116            d['pqr']
117
118            d['abc'] = 1
119            d['def'] = 2
120            d['ghi'] = 3
121            d['jkl'] = 4
122            d['mno'] = 5
123            d['pqr'] = 6
124
125            d['abc']
126            d['def']
127            d['ghi']
128            d['jkl']
129            d['mno']
130            d['pqr']
131
132            d['abc'] = 1
133            d['def'] = 2
134            d['ghi'] = 3
135            d['jkl'] = 4
136            d['mno'] = 5
137            d['pqr'] = 6
138
139            d['abc']
140            d['def']
141            d['ghi']
142            d['jkl']
143            d['mno']
144            d['pqr']
145
146            d['abc'] = 1
147            d['def'] = 2
148            d['ghi'] = 3
149            d['jkl'] = 4
150            d['mno'] = 5
151            d['pqr'] = 6
152
153            d['abc']
154            d['def']
155            d['ghi']
156            d['jkl']
157            d['mno']
158            d['pqr']
159
160    def calibrate(self):
161
162        d = {}
163
164        for i in xrange(self.rounds):
165            pass
166
167class DictWithFloatKeys(Test):
168
169    version = 2.0
170    operations = 5*(6 + 6)
171    rounds = 150000
172
173    def test(self):
174
175        d = {}
176
177        for i in xrange(self.rounds):
178
179            d[1.234] = 1
180            d[2.345] = 2
181            d[3.456] = 3
182            d[4.567] = 4
183            d[5.678] = 5
184            d[6.789] = 6
185
186            d[1.234]
187            d[2.345]
188            d[3.456]
189            d[4.567]
190            d[5.678]
191            d[6.789]
192
193            d[1.234] = 1
194            d[2.345] = 2
195            d[3.456] = 3
196            d[4.567] = 4
197            d[5.678] = 5
198            d[6.789] = 6
199
200            d[1.234]
201            d[2.345]
202            d[3.456]
203            d[4.567]
204            d[5.678]
205            d[6.789]
206
207            d[1.234] = 1
208            d[2.345] = 2
209            d[3.456] = 3
210            d[4.567] = 4
211            d[5.678] = 5
212            d[6.789] = 6
213
214            d[1.234]
215            d[2.345]
216            d[3.456]
217            d[4.567]
218            d[5.678]
219            d[6.789]
220
221            d[1.234] = 1
222            d[2.345] = 2
223            d[3.456] = 3
224            d[4.567] = 4
225            d[5.678] = 5
226            d[6.789] = 6
227
228            d[1.234]
229            d[2.345]
230            d[3.456]
231            d[4.567]
232            d[5.678]
233            d[6.789]
234
235            d[1.234] = 1
236            d[2.345] = 2
237            d[3.456] = 3
238            d[4.567] = 4
239            d[5.678] = 5
240            d[6.789] = 6
241
242            d[1.234]
243            d[2.345]
244            d[3.456]
245            d[4.567]
246            d[5.678]
247            d[6.789]
248
249    def calibrate(self):
250
251        d = {}
252
253        for i in xrange(self.rounds):
254            pass
255
256class DictWithIntegerKeys(Test):
257
258    version = 2.0
259    operations = 5*(6 + 6)
260    rounds = 200000
261
262    def test(self):
263
264        d = {}
265
266        for i in xrange(self.rounds):
267
268            d[1] = 1
269            d[2] = 2
270            d[3] = 3
271            d[4] = 4
272            d[5] = 5
273            d[6] = 6
274
275            d[1]
276            d[2]
277            d[3]
278            d[4]
279            d[5]
280            d[6]
281
282            d[1] = 1
283            d[2] = 2
284            d[3] = 3
285            d[4] = 4
286            d[5] = 5
287            d[6] = 6
288
289            d[1]
290            d[2]
291            d[3]
292            d[4]
293            d[5]
294            d[6]
295
296            d[1] = 1
297            d[2] = 2
298            d[3] = 3
299            d[4] = 4
300            d[5] = 5
301            d[6] = 6
302
303            d[1]
304            d[2]
305            d[3]
306            d[4]
307            d[5]
308            d[6]
309
310            d[1] = 1
311            d[2] = 2
312            d[3] = 3
313            d[4] = 4
314            d[5] = 5
315            d[6] = 6
316
317            d[1]
318            d[2]
319            d[3]
320            d[4]
321            d[5]
322            d[6]
323
324            d[1] = 1
325            d[2] = 2
326            d[3] = 3
327            d[4] = 4
328            d[5] = 5
329            d[6] = 6
330
331            d[1]
332            d[2]
333            d[3]
334            d[4]
335            d[5]
336            d[6]
337
338    def calibrate(self):
339
340        d = {}
341
342        for i in xrange(self.rounds):
343            pass
344
345class SimpleDictManipulation(Test):
346
347    version = 2.0
348    operations = 5*(6 + 6 + 6 + 6)
349    rounds = 100000
350
351    def test(self):
352
353        d = {}
354        has_key = d.has_key
355
356        for i in xrange(self.rounds):
357
358            d[0] = 3
359            d[1] = 4
360            d[2] = 5
361            d[3] = 3
362            d[4] = 4
363            d[5] = 5
364
365            x = d[0]
366            x = d[1]
367            x = d[2]
368            x = d[3]
369            x = d[4]
370            x = d[5]
371
372            has_key(0)
373            has_key(2)
374            has_key(4)
375            has_key(6)
376            has_key(8)
377            has_key(10)
378
379            del d[0]
380            del d[1]
381            del d[2]
382            del d[3]
383            del d[4]
384            del d[5]
385
386            d[0] = 3
387            d[1] = 4
388            d[2] = 5
389            d[3] = 3
390            d[4] = 4
391            d[5] = 5
392
393            x = d[0]
394            x = d[1]
395            x = d[2]
396            x = d[3]
397            x = d[4]
398            x = d[5]
399
400            has_key(0)
401            has_key(2)
402            has_key(4)
403            has_key(6)
404            has_key(8)
405            has_key(10)
406
407            del d[0]
408            del d[1]
409            del d[2]
410            del d[3]
411            del d[4]
412            del d[5]
413
414            d[0] = 3
415            d[1] = 4
416            d[2] = 5
417            d[3] = 3
418            d[4] = 4
419            d[5] = 5
420
421            x = d[0]
422            x = d[1]
423            x = d[2]
424            x = d[3]
425            x = d[4]
426            x = d[5]
427
428            has_key(0)
429            has_key(2)
430            has_key(4)
431            has_key(6)
432            has_key(8)
433            has_key(10)
434
435            del d[0]
436            del d[1]
437            del d[2]
438            del d[3]
439            del d[4]
440            del d[5]
441
442            d[0] = 3
443            d[1] = 4
444            d[2] = 5
445            d[3] = 3
446            d[4] = 4
447            d[5] = 5
448
449            x = d[0]
450            x = d[1]
451            x = d[2]
452            x = d[3]
453            x = d[4]
454            x = d[5]
455
456            has_key(0)
457            has_key(2)
458            has_key(4)
459            has_key(6)
460            has_key(8)
461            has_key(10)
462
463            del d[0]
464            del d[1]
465            del d[2]
466            del d[3]
467            del d[4]
468            del d[5]
469
470            d[0] = 3
471            d[1] = 4
472            d[2] = 5
473            d[3] = 3
474            d[4] = 4
475            d[5] = 5
476
477            x = d[0]
478            x = d[1]
479            x = d[2]
480            x = d[3]
481            x = d[4]
482            x = d[5]
483
484            has_key(0)
485            has_key(2)
486            has_key(4)
487            has_key(6)
488            has_key(8)
489            has_key(10)
490
491            del d[0]
492            del d[1]
493            del d[2]
494            del d[3]
495            del d[4]
496            del d[5]
497
498    def calibrate(self):
499
500        d = {}
501        has_key = d.has_key
502
503        for i in xrange(self.rounds):
504            pass
505