DotNetKick.com is an open-source project. Please report any bugs and let us know your great suggestions. Currently running svn revision 620 (rss)

Kick Spy!, Kick Zeitgeist and Kick Widgets

24
kicks
published 1 year, 8 months ago, submitted by BlackWasp 1 year, 8 months ago

blackwasp.co.uk — The .NET framework and the C# language provide two methods for conditional processing where multiple discrete values can be selected from. The switch statement is less flexible than the if-else-if ladder but is generally considered to be more efficient.

Add a comment 5 comments | category: | Views: 70 | Get KickIt image code
tags: , | tag it

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:

Comments:
Lemme guess... about the same, because they compile to very similar IL?
posted by yesthatmcgurk yesthatmcgurk 1 year, 8 months ago
DOH!
posted by yesthatmcgurk yesthatmcgurk 1 year, 8 months ago
Interesting. For the impatient:

The results show that the switch statement is faster to execute than the if-else-if ladder. This is due to the compiler's ability to optimise the switch statement. In the case of the if-else-if ladder, the code must process each if statement in the order determined by the programmer. However, because each case within a switch statement does not rely on earlier cases, the compiler is able to re-order the testing in such a way as to provide the fastest execution.
posted by senfo 1 year, 8 months ago
Interesting result, but I'm baffled by the closing statement:

This is due to the compiler's ability to optimise the switch statement. In the case of the if-else-if ladder, the code must process each if statement in the order determined by the programmer. However, because each case within a switch statement does not rely on earlier cases, the compiler is able to re-order the testing in such a way as to provide the fastest execution.

Does that mean re-ordering the if/else statements would allow the same performance to be on par with the switch statement? And how could the compiler possibly know the best order for the switch cases when the input is random? At a guess, I'd say using switch is faster because it results in less assembly instructions running, not because of the order of the tests. Wish I had the time to look into it further . . .
posted by kentcb 1 year, 7 months ago
The final statement in the article alludes to the compiler’s ability to optimise the code. With a series of if statements, the executing code is forced down a decision path that may include complex comparisons rather than simple matches.

In the case of the switch statement, fall-throughs are not permitted (unlike in C++) and only matching of single, explicit values occurs. This provides the compiler with the option to optimise the ‘table’ of available match values in a similar manner to a simplified SQL indexing operation.

An example of this in operation with the initial code and IL code can be found at http://www.codeproject.com/cs/algorithms/soundex.asp
posted by BlackWasp 1 year, 7 months ago



information Login or create an account to comment on this story
 

Sponsored Link: www.carlist.ie

Search:

Ads via The Lounge