- 1
Any rule of thumb for using switch vs if-else for performance reasons? Basically like let's say if you need to use switch case if there are more than 4 conditions?
2 Respuestas
+ 3
It seems that the compiler is better in optimizing a switch-statement than an if-statement.
Check this out: http://www.blackwasp.co.uk/SpeedTestIfElseSwitch.aspx
0
Switch Case has better efficiency and is faster and it uses a jump table method to reach the required block to execute.
If-else uses different technique!
In "if" statement, we can easily provide a range condition (for example: age>=18 && age<=45).
There is no such option when using switch case: We can only provide a particular value while using switch case.
Both have their own importance in different types of programs.