Should I use "if" or "try" (as a matter of speed)?

Jorey Bump devnull at joreybump.com
Sun Jul 10 00:17:53 EDT 2005


Steve Juranich <sjuranic at gmail.com> wrote in 
news:mailman.1544.1120933273.10512.python-list at python.org:

> I was wondering how true this holds for Python, where exceptions are
> such an integral part of the execution model.  It seems to me, that if
> I'm executing a loop over a bunch of items, and I expect some
> condition to hold for a majority of the cases, then a "try" block
> would be in order, since I could eliminate a bunch of potentially
> costly comparisons for each item.  But in cases where I'm only trying
> a single getattr (for example), using "if" might be a cheaper way to
> go.
> 
> What do I mean by "cheaper"?  I'm basically talking about the number
> of instructions that are necessary to set up and execute a try block
> as opposed to an if block.
> 
> Could you please tell me if I'm even remotely close to understanding
> this correctly?

*If* I'm not doing a lot of things once, I *try* to do one thing a lot. 




More information about the Python-list mailing list