control structures (was "Re: Sins")

Gareth McCaughan Gareth.McCaughan at pobox.com
Mon Jan 10 18:14:05 EST 2000


Grant Edwards wrote:

> At least with the compilers I've used, that depends on the case
> values.  If the switch case values are sparse, the code
> generated is the same as it would be for If/elif.
> 
> For example:
..
> May generate table-lookup code that is O(1), but
> 
>   switch (c)
>     {
>     case      0: [...] break;
>     case   3450: [...] break;
>     case -49393: [...] break;
>     case 233450: [...] break;
>     case -14000: [...] break;
>     case  -9834: [...] break;
>     }
>     
> Would hopefully generate O(n) linear-search code.

It ought to generate code that does something more
like a binary chop. Looking at the output of gcc -O2
on a big random switch, that's certainly what gcc
does.

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
sig under construction



More information about the Python-list mailing list