Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
[python.git] / Tools / pybench / With.py
blob3af24cc6b7e67f2ef21576947aeddfd8296cb0f0
1 from __future__ import with_statement
2 from pybench import Test
4 class WithFinally(Test):
6 version = 2.0
7 operations = 20
8 rounds = 80000
10 class ContextManager(object):
11 def __enter__(self):
12 pass
13 def __exit__(self, exc, val, tb):
14 pass
16 def test(self):
18 cm = self.ContextManager()
20 for i in xrange(self.rounds):
21 with cm: pass
22 with cm: pass
23 with cm: pass
24 with cm: pass
25 with cm: pass
26 with cm: pass
27 with cm: pass
28 with cm: pass
29 with cm: pass
30 with cm: pass
31 with cm: pass
32 with cm: pass
33 with cm: pass
34 with cm: pass
35 with cm: pass
36 with cm: pass
37 with cm: pass
38 with cm: pass
39 with cm: pass
40 with cm: pass
42 def calibrate(self):
44 cm = self.ContextManager()
46 for i in xrange(self.rounds):
47 pass
50 class TryFinally(Test):
52 version = 2.0
53 operations = 20
54 rounds = 80000
56 class ContextManager(object):
57 def __enter__(self):
58 pass
59 def __exit__(self):
60 # "Context manager" objects used just for their cleanup
61 # actions in finally blocks usually don't have parameters.
62 pass
64 def test(self):
66 cm = self.ContextManager()
68 for i in xrange(self.rounds):
69 cm.__enter__()
70 try: pass
71 finally: cm.__exit__()
73 cm.__enter__()
74 try: pass
75 finally: cm.__exit__()
77 cm.__enter__()
78 try: pass
79 finally: cm.__exit__()
81 cm.__enter__()
82 try: pass
83 finally: cm.__exit__()
85 cm.__enter__()
86 try: pass
87 finally: cm.__exit__()
89 cm.__enter__()
90 try: pass
91 finally: cm.__exit__()
93 cm.__enter__()
94 try: pass
95 finally: cm.__exit__()
97 cm.__enter__()
98 try: pass
99 finally: cm.__exit__()
101 cm.__enter__()
102 try: pass
103 finally: cm.__exit__()
105 cm.__enter__()
106 try: pass
107 finally: cm.__exit__()
109 cm.__enter__()
110 try: pass
111 finally: cm.__exit__()
113 cm.__enter__()
114 try: pass
115 finally: cm.__exit__()
117 cm.__enter__()
118 try: pass
119 finally: cm.__exit__()
121 cm.__enter__()
122 try: pass
123 finally: cm.__exit__()
125 cm.__enter__()
126 try: pass
127 finally: cm.__exit__()
129 cm.__enter__()
130 try: pass
131 finally: cm.__exit__()
133 cm.__enter__()
134 try: pass
135 finally: cm.__exit__()
137 cm.__enter__()
138 try: pass
139 finally: cm.__exit__()
141 cm.__enter__()
142 try: pass
143 finally: cm.__exit__()
145 cm.__enter__()
146 try: pass
147 finally: cm.__exit__()
149 def calibrate(self):
151 cm = self.ContextManager()
153 for i in xrange(self.rounds):
154 pass
157 class WithRaiseExcept(Test):
159 version = 2.0
160 operations = 2 + 3 + 3
161 rounds = 100000
163 class BlockExceptions(object):
164 def __enter__(self):
165 pass
166 def __exit__(self, exc, val, tb):
167 return True
169 def test(self):
171 error = ValueError
172 be = self.BlockExceptions()
174 for i in xrange(self.rounds):
175 with be: raise error
176 with be: raise error
177 with be: raise error,"something"
178 with be: raise error,"something"
179 with be: raise error,"something"
180 with be: raise error("something")
181 with be: raise error("something")
182 with be: raise error("something")
184 def calibrate(self):
186 error = ValueError
187 be = self.BlockExceptions()
189 for i in xrange(self.rounds):
190 pass