views:

73

answers:

3

Generally is there a performance difference between using an if block with many else ifs compared to a switch case block? Do some languages or style conventions prefer one over the other?

Specifically I am curious about Matlab, C, and C++

+1  A: 

In C, the optimizer can turn a switch into a calculated jmp, so it can be faster. This is possible because you always switch on an integer constant.

Lou Franco
+3  A: 

A switch can be optimized by the compiler, in some cases, into a lookup table and branch. This could be considerably faster than multiple if/else-ifs.

Michael Goldshteyn
A: 

read if yu have time http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx

http://channel9.msdn.com/forums/TechOff/8393-Best-Practice-switch-vs-if-questions/

http://weblogs.asp.net/jonyates/archive/2004/03/23/94558.aspx

zod
why downvote man?? how can iknow who is downvoting?
zod
I'm not the downvoter -- but I can see the reason for it. Links can die. You should at least quote some relative information from your references so SO has the information as well. Other than that, perhaps it was because of the misspelling of "you" or the lack of capitalization or poor sentence structure.
Austin Salonen