问题
From what I have read I can summarize,
- Switch case is implementation defined but is mostly defined as a jump table
- Switch case makes the code more readable
- Switch is faster than
if/elseif
(?)
Consider a case where I have 300+ switch cases. I know an if/elseif
in this scene will be a mess.
But I want to know how will a switch
case perform in
such a scene?
- Is it scalable i.e it remains relatively faster than an if/else no matter how many cases are present ?
- Since it is implementation defined how can I figure out how my compiler is implementing it?
- And above all how do I do this
if/elseif
-switch
comparison apart from actually writing the code and using a profiler? I have tried compiling a small.c
file with switch case usinggcc 4.8.1
-S
switch and it looks like a jump table is created.Where do I go from here? - Is it better/worse to use an
if/elseif
in such scenarios
I am primarily interested in C/C++ specific details
回答1:
The compiler might decide to use a jump table and make a huge improvement in the case of 300+.
Compilers do make optimizations over branches using various techniques like decision trees .
The more easy is for a compiler to understand the code, the better. And the switch statement is more readable for the compiler as well.
Think about the else if from a compilers point of view . It would look like an arrowhead :
- if
- else
- else
- else
- else
You need to evaluate each previous if in order to get to the correct else .
However a Switch looks more like a block :
- case
- case
- case
- case
So the compiler can sometimes determine where to go directly.
For your bullet questions :
it is scalable. It's easy to be written by the developers and if the compiler uses jump tables adding more cases won't affect.
it's up to the compiler to decide what to use. It might choose to not optimize it at all (but most likely jump tables).
You can run a loop and meassure times by hand maybe ?
It's always better to use switch. The worst case scenario the switch will act just as an if/else .
回答2:
Compilers for most of the low end processors(Mostly Used in Embedded Systems) compiler do not always generate jump table for switch case.
If the case variables are in sequence (e.g 1,2,3,4....) then jump table implementation of switch case is preferred by compiler ,but for random sequence of case variable(e.g. 12,344,565,1,5...) compiler generates same code as generated in case of if-else code.
Sometimes,due to this developers land into trouble when adding a random case variable into already OK code may change the whole implementation of the that section of code which can result major change in code execution timing and code size. These are most concerning point to a embedded developer.
回答3:
An if else is better than a switch case in the event that you have few choices, with deep decision trees, or need to use non-specific decision making,
ie. it's easy to implement an if statement that checks whether an int is greater than 0, which is more difficult to build into a switch statement. This is the type of logic that is best implemented using if statements.
If you have a flat, very broad, but specific condition dependent decision table, then a switch case is better. For example, you want specific things to happen if when a value is either 1, 2, 3, 4...etc to 10, it will be easier and saner to use a switch case.
In the end, modern compilers will change it to whatever is most efficient computationally, but how you built it will affect functionality, and supportability once you build it.
回答4:
From the question of comparing a switch with an if..then..else construct, ~I assume that you are only cocnerned with a situation where a single test is required and the outcome depends on the answer (eg if x == ?? then ...). It is always better to use a switch in this case since: a) you cannnot introduce an erroneous condition part way down the chain of tests. b) the test is only undertaken once.
来源:https://stackoverflow.com/questions/28339981/at-what-stage-does-an-if-else-become-better-than-a-switch-case-does-it