The rap against "while True:" loops

Jorgen Grahn grahn+nntp at snipabacken.se
Wed Oct 21 17:52:31 EDT 2009


On Wed, 2009-10-14, Steven D'Aprano wrote:
...

> Setting up a try...except block is cheap in Python. According to my 
> tests, the overhead is little more than that of a single pass statement.
>
> But actually raising and catching the exception is not cheap. If you use 
> a lot of exceptions for flow control, performance will probably suffer.

You seem to have experimented with this, so you might be right.

> In C++, exceptions are expensive, whether you catch one or not.

I am not sure that is objectively true, even if you consider that
"expensive" among C++ users often means "costs more than a semi-decent
alternative".  For example, Stroustrup claimed back in 1994 that the
non-catching case can be implemented at no speed cost or no memory
usage cost (Design and Evolution of C++, 1994, p397).

/Jorgen

-- 
  // Jorgen Grahn <grahn@  Oo  o.   .  .
\X/     snipabacken.se>   O  o   .



More information about the Python-list mailing list