Where is CASE?????

David Bolen db3l at fitlinxx.com
Tue Feb 27 20:33:50 EST 2001


Larry Smith <larry at smith-house.org> writes:

> Case statements can be modelled with if-else chains
> but they are not the same internally.  Case statements
> are usually compiled as jump tables, rather than as a
> series of if-elses.  This is hugely faster than if-else
> on average.

It's also what often restricts the case statements to integral values
within some range, and the compilation can be less efficient if your
values are sparsely spread over a very large range (in some cases the
compiler may even end up with the same code as an if-else chain).

But in such a case (lookups), dictionaries in Python are very
efficient for lookups and can offer similar performance advantages,
which still being generic in terms of what is being looked up.

> However, in Guido's defense, I will also point out the
> primary use of case statements is to select alternatives
> based on a type flag - which is not needed in an object
> oriented language.

Well, either that, or keeping the list of alternatives in the
dictionary keyed by the type flag is a nice fit.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list