Click here to Skip to main content
15,903,362 members
Home / Discussions / C#
   

C#

 
AnswerRe: The Switch Statement Pin
Anthony Mushrow13-Oct-08 8:22
professionalAnthony Mushrow13-Oct-08 8:22 
GeneralRe: The Switch Statement Pin
Jammer13-Oct-08 8:28
Jammer13-Oct-08 8:28 
AnswerRe: The Switch Statement Pin
Pete O'Hanlon13-Oct-08 8:31
mvePete O'Hanlon13-Oct-08 8:31 
GeneralRe: The Switch Statement Pin
Jammer13-Oct-08 8:44
Jammer13-Oct-08 8:44 
GeneralRe: The Switch Statement Pin
Jakob Olsen14-Oct-08 1:05
Jakob Olsen14-Oct-08 1:05 
AnswerRe: The Switch Statement Pin
Mark Salsbery13-Oct-08 8:34
Mark Salsbery13-Oct-08 8:34 
GeneralRe: The Switch Statement Pin
Jammer13-Oct-08 8:43
Jammer13-Oct-08 8:43 
GeneralRe: The Switch Statement Pin
Mark Salsbery13-Oct-08 9:15
Mark Salsbery13-Oct-08 9:15 
IMO it comes down to the resulting compiled code.

The ability of a computer to branch based on a decision is
powerful, and taken for granted these days.

So, when designing a high level language, where you want the
ability to branch based on a list of comparisons, what are the
alternatives? One can do it explicitly with if/then/else but that
can be inefficient for the decision at the end of a big list.
To avoid that inefficiency some kind of look-up table may be more efficient.
Theoretically, using the switch semantics, a good compiler could
recognize a big list and produce more efficient branching code - possibly
using a look-up table of some kind. The restrictions of the switch statement
allows this.

Here's a C++ example ( completely OT for this forum, but demonstrates my point):
int i = 6;

if (i == 0)
    TRACE0("0");
else if (i == 1)
    TRACE0("1");
else if (i == 2)
    TRACE0("2");
else if (i == 3)
    TRACE0("3");
else if (i == 4)
    TRACE0("4");
else if (i == 5)
    TRACE0("5");
else if (i == 6)
    TRACE0("6");
else
    TRACE0("n");

switch (i)
{
case 0:
    TRACE0("0");
    break;
case 1:
    TRACE0("1");
    break;
case 2:
    TRACE0("2");
    break;
case 3:
    TRACE0("3");
    break;
case 4:
    TRACE0("4");
    break;
case 5:
    TRACE0("5");
    break;
case 6:
    TRACE0("6");
    break;
default:
    TRACE0("n");
    break;
}

The resulting if/else machine code does six comparisons to get
to the resulting branch.

The resulting switch machine code does one comparison to see if
i is > 6, and if it isn't, branches directly to the correct case
using only TWO machine code instructions (via a look-up table)
0041D9F5  mov         ecx,dword ptr [ebp-0AE4h] 
0041D9FB  jmp         dword ptr  (41F8CCh)[ecx*4] 

switch may be ugly in source code, but the compiler can certainly
take advantage of it to produce efficient code.

And I don't buy the "purist programmer" arguments...what purist would
use C# (or any other higher level language)? Wink | ;)


It still all comes down to knowing the language you're using and
choosing the right instructions for the given situation...

Mark

Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

GeneralRe: The Switch Statement Pin
Jammer13-Oct-08 9:18
Jammer13-Oct-08 9:18 
GeneralRe: The Switch Statement Pin
Mark Salsbery13-Oct-08 9:22
Mark Salsbery13-Oct-08 9:22 
GeneralRe: The Switch Statement Pin
Jammer13-Oct-08 9:26
Jammer13-Oct-08 9:26 
GeneralRe: The Switch Statement Pin
Vengatachalapathy Palanivel13-Oct-08 19:05
Vengatachalapathy Palanivel13-Oct-08 19:05 
GeneralRe: The Switch Statement Pin
Mark Salsbery13-Oct-08 20:23
Mark Salsbery13-Oct-08 20:23 
AnswerRe: The Switch Statement Pin
Wendelius13-Oct-08 8:41
mentorWendelius13-Oct-08 8:41 
AnswerRe: The Switch Statement Pin
PIEBALDconsult13-Oct-08 8:44
mvePIEBALDconsult13-Oct-08 8:44 
GeneralRe: The Switch Statement Pin
Guffa13-Oct-08 11:42
Guffa13-Oct-08 11:42 
GeneralRe: The Switch Statement Pin
PIEBALDconsult13-Oct-08 16:04
mvePIEBALDconsult13-Oct-08 16:04 
AnswerRe: The Switch Statement - Quality Anyone? Pin
carbon_golem13-Oct-08 10:05
carbon_golem13-Oct-08 10:05 
GeneralRe: The Switch Statement - Quality Anyone? Pin
led mike13-Oct-08 10:08
led mike13-Oct-08 10:08 
GeneralRe: The Switch Statement - Quality Anyone? Pin
#realJSOP13-Oct-08 10:57
professional#realJSOP13-Oct-08 10:57 
AnswerRe: The Switch Statement [modified] Pin
led mike13-Oct-08 10:06
led mike13-Oct-08 10:06 
GeneralRe: The Switch Statement Pin
Mark Salsbery13-Oct-08 11:55
Mark Salsbery13-Oct-08 11:55 
GeneralRe: The Switch Statement Pin
led mike13-Oct-08 15:36
led mike13-Oct-08 15:36 
AnswerRe: The Switch Statement Pin
#realJSOP13-Oct-08 10:54
professional#realJSOP13-Oct-08 10:54 
GeneralRe: The Switch Statement Pin
Jammer13-Oct-08 11:01
Jammer13-Oct-08 11:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.