• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import with_statement
2from pybench import Test
3
4class WithFinally(Test):
5
6    version = 2.0
7    operations = 20
8    rounds = 80000
9
10    class ContextManager(object):
11        def __enter__(self):
12            pass
13        def __exit__(self, exc, val, tb):
14            pass
15
16    def test(self):
17
18        cm = self.ContextManager()
19
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
41
42    def calibrate(self):
43
44        cm = self.ContextManager()
45
46        for i in xrange(self.rounds):
47            pass
48
49
50class TryFinally(Test):
51
52    version = 2.0
53    operations = 20
54    rounds = 80000
55
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
63
64    def test(self):
65
66        cm = self.ContextManager()
67
68        for i in xrange(self.rounds):
69            cm.__enter__()
70            try: pass
71            finally: cm.__exit__()
72
73            cm.__enter__()
74            try: pass
75            finally: cm.__exit__()
76
77            cm.__enter__()
78            try: pass
79            finally: cm.__exit__()
80
81            cm.__enter__()
82            try: pass
83            finally: cm.__exit__()
84
85            cm.__enter__()
86            try: pass
87            finally: cm.__exit__()
88
89            cm.__enter__()
90            try: pass
91            finally: cm.__exit__()
92
93            cm.__enter__()
94            try: pass
95            finally: cm.__exit__()
96
97            cm.__enter__()
98            try: pass
99            finally: cm.__exit__()
100
101            cm.__enter__()
102            try: pass
103            finally: cm.__exit__()
104
105            cm.__enter__()
106            try: pass
107            finally: cm.__exit__()
108
109            cm.__enter__()
110            try: pass
111            finally: cm.__exit__()
112
113            cm.__enter__()
114            try: pass
115            finally: cm.__exit__()
116
117            cm.__enter__()
118            try: pass
119            finally: cm.__exit__()
120
121            cm.__enter__()
122            try: pass
123            finally: cm.__exit__()
124
125            cm.__enter__()
126            try: pass
127            finally: cm.__exit__()
128
129            cm.__enter__()
130            try: pass
131            finally: cm.__exit__()
132
133            cm.__enter__()
134            try: pass
135            finally: cm.__exit__()
136
137            cm.__enter__()
138            try: pass
139            finally: cm.__exit__()
140
141            cm.__enter__()
142            try: pass
143            finally: cm.__exit__()
144
145            cm.__enter__()
146            try: pass
147            finally: cm.__exit__()
148
149    def calibrate(self):
150
151        cm = self.ContextManager()
152
153        for i in xrange(self.rounds):
154            pass
155
156
157class WithRaiseExcept(Test):
158
159    version = 2.0
160    operations = 2 + 3 + 3
161    rounds = 100000
162
163    class BlockExceptions(object):
164        def __enter__(self):
165            pass
166        def __exit__(self, exc, val, tb):
167            return True
168
169    def test(self):
170
171        error = ValueError
172        be = self.BlockExceptions()
173
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")
183
184    def calibrate(self):
185
186        error = ValueError
187        be = self.BlockExceptions()
188
189        for i in xrange(self.rounds):
190            pass
191