control structures (was "Re: Sins")
Grant Edwards
grant at nowhere.
Mon Jan 10 12:04:53 EST 2000
In article <38774FBC.EB8281CF at maxtal.com.au>, skaller wrote:
>Skip Montanaro wrote:
>>
>> Darrell> Don't know if this if fair. But exceptions are slow when raised.
>>
>> Agreed in the case of your two-branch case. If you have a state machine
>> with a bunch of states, try/except/except/... appears to perform about as
>> well as if/elif/elif/.../else:
>
> Yeah, but this is an artefact, since Python does not
>have a proper switch statement. If/elif ... is clearly
>linear time .. a switch is constant time.
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:
switch (c)
{
case 0: [...] break;
case 1: [...] break;
case 2: [...] break;
case 3: [...] break;
case 4: [...] break;
case 5: [...] break;
case 6: [...] break;
case 7: [...] break;
}
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.
For FSM code, one can probably chose non-sparse switch case
values for everything...
--
Grant Edwards grante Yow! .. he dominates the
at DECADENT SUBWAY SCENE.
visi.com
More information about the Python-list
mailing list