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