The rap against "while True:" loops

Hendrik van Rooyen hendrik at microcorp.co.za
Tue Oct 20 03:25:18 EDT 2009


On Monday, 19 October 2009 09:43:15 Steven D'Aprano wrote:
> On Mon, 19 Oct 2009 08:51:44 +0200, Hendrik van Rooyen wrote:
> > The point I was trying to make
> > subliminally, was that there is a relative cost of double lookup for all
> > cases versus exceptions for some cases. - Depending on the frequency of
> > "some", I would expect a breakeven point.
>
> There is, at least according to my (long distant and only barely
> remembered) tests.

8< -----------------------------------
>
> under Python 2.5. However, catching an exception is more expensive,
> approximately ten times more so. Doing a lookup twice falls somewhere
> between the two, closer to the cheap side than the expensive.
>
> So according to my rough estimates, it is faster to use the try...except
> form so long as the number of KeyErrors is less than about one in six,
> give or take. If KeyError is more common than that, it's cheaper to do a
> test first, say with d.has_key(). Using the `in` operator is likely to be
> faster than has_key(), which will shift the break-even point.
>
> (The above numbers are from memory and should be taken with a large pinch
> of salt. Even if they are accurate for me, they will likely be different
> on other machines, and will depend on the actual keys in the dict. In
> other words, your mileage may vary.)

So if you want to sum stuff where there are a lot of keys, but only a few 
values per key - say between one and ten, then it would be faster to look 
before you leap.

On the other hand, if there are relatively few keys and tens or hundreds of 
values per key, then you ask for forgiveness.

And if you don't  know what the data is going to look like, then you should 
either go into a catatonic state, or take to the bottle, as the zen of python 
states that you should refuse the temptation to guess.

This is also known as paralysis by analysis.

:-)

- Hendrik






More information about the Python-list mailing list