[Python-Dev] cpython: Rename contextlib.ignored() to contextlib.ignore().

Antoine Pitrou solipsis at pitrou.net
Wed Oct 16 14:24:11 CEST 2013


Le Wed, 16 Oct 2013 14:09:16 +0200,
Antoine Pitrou <solipsis at pitrou.net> a écrit :
> Le Wed, 16 Oct 2013 14:01:37 +0200,
> Victor Stinner <victor.stinner at gmail.com> a écrit :
> > 2013/10/16 Antoine Pitrou <solipsis at pitrou.net>:
> > >> By the way, what are the performances of contextlib.ignore()?
> > >> Exceptions can be slow in some cases. Adding something even
> > >> slower would not be a good idea.
> > >
> > > A "try" block which succeeds is fast.
> > 
> > Ah yes, I never reminder this fact. I try to not care too much of
> > micro-optimizations :-)
> 
> It's not so much a micro-optimization than the fact that pushing a
> block on the stack is very cheap.
> IIRC, what is expensive is:
> - creating the exception object with the associated traceback
> - matching the exception in the "exception SomeException" clause

By the way, of course this must be kept in proportion. At worse the
total cost is a couple of microseconds.
Still, a simplistic micro-benchmark:

$ ./python -m timeit "try: pass" "except Exception: pass"
100000000 loops, best of 3: 0.0164 usec per loop

$ ./python -m timeit -s "from contextlib import ignore" "with ignore(Exception): pass"
100000 loops, best of 3: 2.21 usec per loop

Regards

Antoine.




More information about the Python-Dev mailing list