The rap against "while True:" loops

Steven D'Aprano steven at
Mon Oct 19 09:43:15 CEST 2009

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.

Setting up a try...except is very cheap, about as cheap as a pass 
statement. That is:

d = {1: None}
    x = d[1]
except KeyError:
    print "This can't happen"

is approximately as costly as:

d = {1: None}
x = d[1]

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.)


More information about the Python-list mailing list