[Python-Dev] Performance of pre-creating exceptions?
Guido van Rossum
guido at python.org
Sat Mar 3 05:52:45 CET 2007
Often people build mental models of performance that have little
bearing on reality. Thanks for measuring!
On 3/2/07, Adam Olsen <rhamph at gmail.com> wrote:
> $ python2.5 -m timeit -r 10 -n 1000000 -s 'class Foo(Exception): pass'
> 'try: raise Foo()' 'except: pass'
> 1000000 loops, best of 10: 2.49 usec per loop
> $ python2.5 -m timeit -r 10 -n 1000000 -s 'class Foo(Exception):' -s '
> def __init__(self): pass' 'try: raise Foo()' 'except: pass'
> 1000000 loops, best of 10: 3.15 usec per loop
> $ python2.5 -m timeit -r 10 -n 1000000 -s 'e = Exception()' 'try:
> raise e' 'except: pass'
> 1000000 loops, best of 10: 2.03 usec per loop
>
> We can get more than half of the benefit simply by using a default
> __init__ rather than a python one. If you need custom attributes but
> they're predefined you could subclass the exception and have them as
> class attributes. Given that, is there really a need to pre-create
> exceptions?
>
> --
> Adam Olsen, aka Rhamphoryncus
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list