from __future__ import with_statementfrom pybench import Testclass WithFinally(Test):version = 2.0operations = 20rounds = 80000class ContextManager(object):def __enter__(self):passdef __exit__(self, exc, val, tb):passdef test(self):cm = self.ContextManager()for i in xrange(self.rounds):with cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passwith cm: passdef calibrate(self):cm = self.ContextManager()for i in xrange(self.rounds):passclass TryFinally(Test):version = 2.0operations = 20rounds = 80000class ContextManager(object):def __enter__(self):passdef __exit__(self):# "Context manager" objects used just for their cleanup# actions in finally blocks usually don't have parameters.passdef test(self):cm = self.ContextManager()for i in xrange(self.rounds):cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()cm.__enter__()try: passfinally: cm.__exit__()def calibrate(self):cm = self.ContextManager()for i in xrange(self.rounds):passclass WithRaiseExcept(Test):version = 2.0operations = 2 + 3 + 3rounds = 100000class BlockExceptions(object):def __enter__(self):passdef __exit__(self, exc, val, tb):return Truedef test(self):error = ValueErrorbe = self.BlockExceptions()for i in xrange(self.rounds):with be: raise errorwith be: raise errorwith be: raise error,"something"with be: raise error,"something"with be: raise error,"something"with be: raise error("something")with be: raise error("something")with be: raise error("something")def calibrate(self):error = ValueErrorbe = self.BlockExceptions()for i in xrange(self.rounds):pass
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。