Click here to Skip to main content
15,891,184 members
Home / Discussions / C#
   

C#

 
Question'switch' statement efficiency in C# Pin
scody30-Jul-08 19:50
scody30-Jul-08 19:50 
AnswerRe: 'switch' statement efficiency in C# Pin
Christian Graus30-Jul-08 20:07
protectorChristian Graus30-Jul-08 20:07 
GeneralRe: 'switch' statement efficiency in C# Pin
scody30-Jul-08 21:12
scody30-Jul-08 21:12 
AnswerRe: 'switch' statement efficiency in C# Pin
Guffa30-Jul-08 22:02
Guffa30-Jul-08 22:02 
GeneralRe: 'switch' statement efficiency in C# Pin
leppie30-Jul-08 22:52
leppie30-Jul-08 22:52 
GeneralRe: 'switch' statement efficiency in C# Pin
Christian Graus30-Jul-08 23:57
protectorChristian Graus30-Jul-08 23:57 
GeneralRe: 'switch' statement efficiency in C# Pin
leppie31-Jul-08 0:14
leppie31-Jul-08 0:14 
GeneralRe: 'switch' statement efficiency in C# Pin
Guffa31-Jul-08 1:57
Guffa31-Jul-08 1:57 
leppie wrote:
It creates the same construct regardless of the number of cases.


Not at all.

Here is the generated code for a switch containing five case labels and one containing six case labels. Do you see the fundamental difference?
00000084  mov         esi,dword ptr [ebp-44h] 
00000087  test        esi,esi 
00000089  je          000000ED 
0000008b  mov         edx,dword ptr ds:[02C33098h] 
00000091  mov         ecx,esi 
00000093  call        7795C574 
00000098  mov         edi,eax 
0000009a  test        edi,edi 
0000009c  jne         000000ED 
0000009e  mov         edx,dword ptr ds:[02C3309Ch] 
000000a4  mov         ecx,esi 
000000a6  call        7795C574 
000000ab  mov         edi,eax 
000000ad  test        edi,edi 
000000af  jne         000000ED 
000000b1  mov         edx,dword ptr ds:[02C330A0h] 
000000b7  mov         ecx,esi 
000000b9  call        7795C574 
000000be  mov         edi,eax 
000000c0  test        edi,edi 
000000c2  jne         000000ED 
000000c4  mov         edx,dword ptr ds:[02C330A4h] 
000000ca  mov         ecx,esi 
000000cc  call        7795C574 
000000d1  mov         edi,eax 
000000d3  test        edi,edi 
000000d5  jne         000000ED 
000000d7  mov         edx,dword ptr ds:[02C330A8h] 
000000dd  mov         ecx,esi 
000000df  call        7795C574 
000000e4  mov         edi,eax 
000000e6  test        edi,edi 
000000e8  jne         000000ED 
000000ea  nop              
000000eb  jmp         000000ED

0000008a  mov         ebx,dword ptr [ebp-44h] 
0000008d  test        ebx,ebx 
0000008f  je          00000148 
00000095  cmp         dword ptr ds:[02B284BCh],0 
0000009c  jne         0000011F 
000000a2  mov         ecx,7913386Ch 
000000a7  call        FCAC0A5C 
000000ac  mov         esi,eax 
000000ae  mov         ecx,esi 
000000b0  mov         edx,6 
000000b5  call        75929474 
000000ba  push        0    
000000bc  mov         edx,dword ptr ds:[02B23098h] 
000000c2  mov         ecx,esi 
000000c4  call        75954260 
000000c9  push        1    
000000cb  mov         edx,dword ptr ds:[02B2309Ch] 
000000d1  mov         ecx,esi 
000000d3  call        75954260 
000000d8  push        2    
000000da  mov         edx,dword ptr ds:[02B230A0h] 
000000e0  mov         ecx,esi 
000000e2  call        75954260 
000000e7  push        3    
000000e9  mov         edx,dword ptr ds:[02B230A4h] 
000000ef  mov         ecx,esi 
000000f1  call        75954260 
000000f6  push        4    
000000f8  mov         edx,dword ptr ds:[02B230A8h] 
000000fe  mov         ecx,esi 
00000100  call        75954260 
00000105  push        5    
00000107  mov         edx,dword ptr ds:[02B230ACh] 
0000010d  mov         ecx,esi 
0000010f  call        75954260 
00000114  lea         edx,ds:[02B284BCh] 
0000011a  call        76234066 
0000011f  lea         eax,[ebp-54h] 
00000122  push        eax  
00000123  mov         ecx,dword ptr ds:[02B284BCh] 
00000129  mov         edx,ebx 
0000012b  call        75789F50 
00000130  mov         esi,eax 
00000132  test        esi,esi 
00000134  je          00000148 
00000136  mov         eax,dword ptr [ebp-54h] 
00000139  cmp         eax,6 
0000013c  jae         00000145 
0000013e  jmp         dword ptr [eax*4+03C41798h] 
00000145  nop              
00000146  jmp         00000148


Despite everything, the person most likely to be fooling you next is yourself.

GeneralRe: 'switch' statement efficiency in C# Pin
leppie31-Jul-08 2:17
leppie31-Jul-08 2:17 
GeneralRe: 'switch' statement efficiency in C# Pin
Guffa31-Jul-08 2:58
Guffa31-Jul-08 2:58 
GeneralRe: 'switch' statement efficiency in C# Pin
leppie31-Jul-08 5:46
leppie31-Jul-08 5:46 
GeneralRe: 'switch' statement efficiency in C# Pin
leppie30-Jul-08 22:51
leppie30-Jul-08 22:51 
GeneralRe: 'switch' statement efficiency in C# Pin
Guffa31-Jul-08 1:18
Guffa31-Jul-08 1:18 
AnswerRe: 'switch' statement efficiency in C# Pin
PIEBALDconsult3-Aug-08 17:07
mvePIEBALDconsult3-Aug-08 17:07 
QuestionConvert a Java timestamp to C# DateTime object Pin
cyrus_virus30-Jul-08 19:26
cyrus_virus30-Jul-08 19:26 
AnswerRe: Convert a Java timestamp to C# DateTime object Pin
Christian Graus30-Jul-08 20:03
protectorChristian Graus30-Jul-08 20:03 
AnswerRe: Convert a Java timestamp to C# DateTime object Pin
Mike Dimmick31-Jul-08 1:16
Mike Dimmick31-Jul-08 1:16 
AnswerRe: Convert a Java timestamp to C# DateTime object Pin
curtisk31-Jul-08 3:49
curtisk31-Jul-08 3:49 
Questiongetting excel cell reference Pin
Mogaambo30-Jul-08 19:19
Mogaambo30-Jul-08 19:19 
Questionproblem to solve the warning!! Pin
ktamanna30-Jul-08 18:33
ktamanna30-Jul-08 18:33 
AnswerRe: problem to solve the warning!! Pin
Christian Graus30-Jul-08 19:39
protectorChristian Graus30-Jul-08 19:39 
AnswerRe: problem to solve the warning!! Pin
Vimalsoft(Pty) Ltd30-Jul-08 19:44
professionalVimalsoft(Pty) Ltd30-Jul-08 19:44 
Questionwhat type of value does switch statement allow to use?such as switch(?){....} Pin
panyl30-Jul-08 16:18
panyl30-Jul-08 16:18 
AnswerRe: what type of value does switch statement allow to use?such as switch(?){....} Pin
Christian Graus30-Jul-08 16:55
protectorChristian Graus30-Jul-08 16:55 
AnswerRe: what type of value does switch statement allow to use?such as switch(?){....} [modified] Pin
DaveyM6930-Jul-08 23:37
professionalDaveyM6930-Jul-08 23:37 

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.