Le Wed, 16 Oct 2013 14:09:16 +0200,
Antoine Pitrou <solip...@pitrou.net> a écrit :
> Le Wed, 16 Oct 2013 14:01:37 +0200,
> Victor Stinner <victor.stin...@gmail.com> a écrit :
> > 2013/10/16 Antoine Pitrou <solip...@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.


_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to