Click here to Skip to main content
15,905,148 members
Home / Discussions / C#
   

C#

 
GeneralRe: Most efficient switch statement variable to use? Pin
Deresen24-Feb-09 10:08
Deresen24-Feb-09 10:08 
GeneralRe: Most efficient switch statement variable to use? Pin
harold aptroot24-Feb-09 11:31
harold aptroot24-Feb-09 11:31 
GeneralRe: Most efficient switch statement variable to use? Pin
Deresen24-Feb-09 12:02
Deresen24-Feb-09 12:02 
GeneralRe: Most efficient switch statement variable to use? Pin
harold aptroot24-Feb-09 12:19
harold aptroot24-Feb-09 12:19 
GeneralRe: Most efficient switch statement variable to use? Pin
Deresen24-Feb-09 12:26
Deresen24-Feb-09 12:26 
GeneralRe: Most efficient switch statement variable to use? Pin
harold aptroot24-Feb-09 13:01
harold aptroot24-Feb-09 13:01 
AnswerRe: Most efficient switch statement variable to use? Pin
Ennis Ray Lynch, Jr.24-Feb-09 8:43
Ennis Ray Lynch, Jr.24-Feb-09 8:43 
AnswerRe: Most efficient switch statement variable to use? [modified] Pin
harold aptroot24-Feb-09 11:23
harold aptroot24-Feb-09 11:23 
Downcasting is often a 0 step operation (no operation actually, but just start using less bits of the register)
It won't become faster though, even operations on 128bits at a time have the same latency and throughput. On Core2 at least.

note: the following part is based on speculation

So would code would the JIT compiler generate for a switch instruction in MSIL? With a bit of bad luck it will generate something like: (assume switch variable is in eax)
mov edx,SwitchTabelBase ;or any other register that can be used as base
jmp [edx+4*eax]
Bad luck because that would mean it will need an extra operation if it sees a downcast to a byte first, it can't just assume that the cast is a nop, unless it would do some expensive analyzes it can't know that the value won't exceed 255 anyway. So it would have to do:
movzx eax,al
mov edx,SwitchTabelBase ;or any other register that can be used as base
jmp [edx+4*eax]
Or possibly:
and eax,0xFF
mov edx,SwitchTabelBase ;or any other register that can be used as base
jmp [edx+4*eax]
Or something else? Who knows? How can you disassemble the code after it's JIT-compiled anyway?

However! (speculations end here)
A switch instruction (in MSIL) is only generated when the resulting table would be dense, when it isn't generated, a 'tree' of if's is generated instead (or a chain of if's as a special case, which the .NET reflector doesn't understand). It isn't really a tree though, it's a mess of if's and labels (in a linear way), but the data-flow as "as though it were a tree of if's". It does the expected thing - split the range of values in 2 every time. Obviously this is a O(log n) algorithm so beware, switch doesn't always perform O(1).

edit: the whole point for this was to note that:
The size of the operand doesn't matter for speed, unless it's bigger than 64bits, because these are comparisons, and a 64bit comparison can be done as fast as any smaller comparison. 32bit if running in 32bit mode. The 128bits thing only works when working with SSE, which the .NET JIT compiler doesn't use, except for FISTTP which is technically an SSE instruction but it works with the regular floating point stack.

For switches on strings it's a whole different story - if a real switch is used, all possible values are put into a dictionary every time again the dictionary is not saved. Otherwise it generated a chain of if's, using op_Equality (aka ==) between the value and every case. Both algorithms are O(n) in the number of cases, but the chain of if's at least has an early exit and doesn't create a (possibly big) dictionary - the downside is that equality test may be slow, depending on the situation.

Last modified: 1hr 46mins after originally posted -- I forgot to include The Point, lol



GeneralRe: Most efficient switch statement variable to use? Pin
harold aptroot25-Feb-09 1:30
harold aptroot25-Feb-09 1:30 
AnswerRe: Most efficient switch statement variable to use? Pin
Mark Churchill24-Feb-09 14:13
Mark Churchill24-Feb-09 14:13 
GeneralRe: Most efficient switch statement variable to use? Pin
PIEBALDconsult24-Feb-09 15:15
mvePIEBALDconsult24-Feb-09 15:15 
GeneralRe: Most efficient switch statement variable to use? Pin
harold aptroot24-Feb-09 15:42
harold aptroot24-Feb-09 15:42 
Questionapp.config file Pin
rcwoods24-Feb-09 6:17
rcwoods24-Feb-09 6:17 
QuestionSaving and loading a file Pin
bar300024-Feb-09 5:31
bar300024-Feb-09 5:31 
AnswerRe: Saving and loading a file Pin
CPallini24-Feb-09 5:39
mveCPallini24-Feb-09 5:39 
AnswerRe: Saving and loading a file Pin
Paul Kettley24-Feb-09 6:12
Paul Kettley24-Feb-09 6:12 
GeneralRe: Saving and loading a file Pin
Jon Rista24-Feb-09 6:21
Jon Rista24-Feb-09 6:21 
AnswerRe: Saving and loading a file Pin
Luc Pattyn24-Feb-09 7:23
sitebuilderLuc Pattyn24-Feb-09 7:23 
GeneralRe: Saving and loading a file Pin
PIEBALDconsult24-Feb-09 7:37
mvePIEBALDconsult24-Feb-09 7:37 
QuestionVSTO Outlook 2007 Pin
staticv24-Feb-09 4:14
staticv24-Feb-09 4:14 
AnswerRe: VSTO Outlook 2007 Pin
Calin Tatar24-Feb-09 4:34
Calin Tatar24-Feb-09 4:34 
GeneralRe: VSTO Outlook 2007 Pin
staticv24-Feb-09 4:40
staticv24-Feb-09 4:40 
QuestionExposing an already running singlton .NET component as COM server Pin
Omer S.24-Feb-09 4:05
Omer S.24-Feb-09 4:05 
Questionstd::string version in C# - Question Pin
Programm3r24-Feb-09 3:34
Programm3r24-Feb-09 3:34 
AnswerRe: std::string version in C# - Question Pin
EliottA24-Feb-09 3:43
EliottA24-Feb-09 3:43 

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.