11

I'd like to know if there is any efficiency difference between using if statement or switch. For example:

if(){
//code
}
else if(){
//code
}
else{
//code
}

I believe that program needs to go and check all of the if statement even if the first if statement was true.

switch(i){

case 1:
//code
break;
case 2:
//code
break;

But in the switch, there is a break command. Is my approaching right? If not, could you explain the efficiency difference between them?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Gökhan Akduğan
  • 533
  • 1
  • 8
  • 17
  • 3
    Possible duplicate of [What is the difference between IF-ELSE and SWITCH?](http://stackoverflow.com/questions/680656/what-is-the-difference-between-if-else-and-switch) – Pradeep Simha Oct 10 '15 at 08:30
  • 1
    "program needs to go and check all of the if statement even if the first if statement was true", why do you think so? What if we use `if`s in place of the `else if`s? – Emil Laine Oct 10 '15 at 08:32
  • 1
    @Simze The question [What is the difference between IF-ELSE and SWITCH?](http://stackoverflow.com/questions/680656/what-is-the-difference-between-if-else-and-switch) and it's answers do not address efficiency in either in java or in an agnostic way. – krock Oct 10 '15 at 08:36
  • 1
    Possible duplicate of [What is the relative performance difference of if/else versus switch statement in Java?](http://stackoverflow.com/questions/2086529/what-is-the-relative-performance-difference-of-if-else-versus-switch-statement-i) – Madhawa Priyashantha Oct 10 '15 at 08:42
  • 1
    http://stackoverflow.com/questions/97987/advantage-of-switch-over-if-else-statement Here they goes into depht of the compilers interpretation of the statements. Possible a duplicate due to the fact the subject has been a subject of discussion multiple times. – mrhn Oct 10 '15 at 08:42
  • No difference; the byte codes used by one or the other are same. – fge Oct 10 '15 at 09:09
  • @fge Really? You've looked? – user207421 Oct 10 '15 at 19:02
  • @ThomasVos When editing, please don't replace one code style with your favorite code style. Such subjective edits are unwanted. See https://meta.stackoverflow.com/questions/263115/when-is-it-okay-to-edit-answers-for-code-formatting And don't just add code that wasn't there before. – Mark Rotteveel Jan 05 '18 at 18:43

3 Answers3

7

Switch perf is better than if else as in case of switch there will be one time evaluation . Once it evaluated the switch it knows which case needs to be executed but in case of if else it has to go through all conditions in case of worst scenario.

The longer the list condition, better will be switch performance but for shorter list (just two conditions), it can be slower also

From Why switch is faster than if

With switch the JVM loads the value to compare and iterates through the value table to find a match, which is faster in most cases

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • It is incorrect to talk about performance gain until you measure it. In most apps performance bottleneck will be anywhere but not in IFs or SWITCHes. – ursa Oct 10 '15 at 08:48
  • Well we don't have to reinvent the wheel, when it has already been, Here OP asks about specific `if else` vs `switch` not something else in performnace – M Sach Oct 10 '15 at 08:57
  • Sure. There is no guaranteed gain. In some cases IFs will be faster then SWITCH. In other cases - visa versa. All performance questions require measurements. – ursa Oct 10 '15 at 09:01
  • @ursa The question isn't about where the OP's bottleneck is. It is about which is faster: if or switch. If you have a comment on the *question,* post it under the question, – user207421 Oct 10 '15 at 19:01
  • @EJP I'm sorry, but I will repeat - you cannot give an answer "A is faster" or "B is faster" until you **measure** a particular case. And answer should contain this notice otherwise it is incorrect. – ursa Oct 10 '15 at 19:21
  • 2
    @ursa is correct. There are plenty of situations where a `switch` will not outperform `if/else`. A `switch` increases the size of a method, making it less likely to be inlined. An `if/else` may perform better if you know which branches are most likely to be taken an can structure it accordingly. Using a `switch` may also affect branch prediction in subtle ways. Test and measure. – Mike Strobel May 24 '17 at 12:51
3

Switch is faster.

Imagine you are at an intersection, with many paths. With switch, you go to the right path at the first time.

With if, then you have to try all the paths before you find the right one.

Use switch whenever possible.

Of course, for computer this difference is very small that you don't even notice. But yeah, you get the point.

Fadils
  • 1,508
  • 16
  • 21
0

I think the code is quite clear. With if, you have to check each case and after case by case (in the worst case, last return gives back the result). With switch, some kind like a special byte code checking and jump to the correct case to return. So the switch is a bit faster than the if statement. However, I think we need to focus on the way we implement for easier to read. In some simple case, the if is also a choice to write code.

Kenny Tai Huynh
  • 1,464
  • 2
  • 11
  • 23