Adding finally to switch

Yigal Chripun yigal100 at gmail.com
Tue Apr 1 14:56:58 PDT 2008


Jesse Phillips wrote:
> On Mon, 31 Mar 2008 21:52:17 -0700, Scott S. McCoy wrote:
>
>   
>> My thought on switch has always been:  If you're using it as multiple if
>> statements, you're misusing it.
>>     
>
> I would agree, except I don't. :) I thought one of the goals in 
> programming was to choose tools that would reduce the number of repeats 
> in code. That is to say if I have several things X could be and want to 
> do something different for all of them, would it be logical to only have 
> to write X once instead of if(X == B) else if (X == C) ...?
>
> Anyway maybe BCS has a good choice for it.
>   
except, you're forgetting one important issue - multi threading:
if you're on a single processor/core/PC/etc, switch would be faster then
a series of if statements, as it could be implemented as a table.
however, on a multi-core/mutli-cpu the exact opposite is true, and it's
more general as in the next snippet:
if (x == 1) do_a();
if (x > 0) do_b();
if (x < 2) do_c();
the above could be run in parallel (providing of course, that do_a, do_b
and do_c are independent) on different cores and it doesn't have to be
deterministic (only one case is run).
in new code meant to be run on multi-core PCs the above is much better
than a switch.

--Yigal

PS - writing the above made me thinking: do we need a special
parallel_switch syntax for code such as the above where all cases are
independent from each other and can run in parallel?



More information about the Digitalmars-d mailing list