FW: Switch statements again

Bengt Richter bokr at oz.net
Thu Jan 16 03:40:23 EST 2003


On Wed, 15 Jan 2003 18:21:40 -0500, Steven Scott <bodoni26 at resnet.gatech.edu> wrote:

>Quoting Steven Scott <Steven.Scott at Synchrologic.com>:
>>=20
>> If/elif/else remains the most common method.  If Python ever gains
>> something
>> like a switch statement you can bet the farm it won't have a "fall thru=
>"
>> feature though, so the way you've coded your C switch statement wouldn'=
>t
>> work.
>>=20
>
>but the absolute /only/ reason you'd use a switch over an if (besides
>looking better) is fall through.
>
Well, if you look at the code for an optimized C switch I think you'll
find another reason. I.e., if you can predetermine that all the switch
case _constants_ form a compact sequence of values, you can generate a
computed jump through a table that is a lot faster than a series of
conditional jumps, and even in cases where you have to introduce conditional
jumps for boundary cases you may still gain.

Regards,
Bengt Richter




More information about the Python-list mailing list