2

This question is more of a theoretical one than a practical one, considering only the performance of multiple if vs. chained if else. Lets leave aside switch, readability, micro-optimizations, error-reducibility, etc.

Let's say I have the following construct:

if( i == 1 ){
  // Do one thing
}
else if( i == 2 ){
  // Do another thing
}
else if( i == 3 ){
  // Do a third thing
}
// ... and so on

I've always assumed that would be more efficient to execute than the following:

if( i == 1 ){
  // Do one thing
}
if( i == 2 ){
  // Do another thing
}
if( i == 3 ){
  // Do a third thing
}
// ... and so on

But is this really so in modern compilers? From what I understand, there are a lot of optimizations and branching strategies that compilers nowadays use on these types of constructs, so perhaps they both actually result in the same executable code?

Magnus
  • 17,157
  • 19
  • 104
  • 189

3 Answers3

3

In addtion to the comment about the duplicate, also keep in mind, that these two snippets have a totally different semantics. Imagine the following

int i = 1;
if (i == 1) {
  i = 2;
} else if (i == 2) {
  i = 3;
}
print(i); //will print 2

vs

int i = 1;
if (i == 1) {
  i = 2;
} 
if (i == 2) {
  i = 3;
}
print(i); //will print 3

I don't think compiler optimization will (or should) always be able to deduce whether an if .. if should be optimized to an if .. else if. It could be possible to do it in this simple example, but changing the state (and therefore the result of the conditions, however bad code style that would be!) could be much more complex, which will make it impossible for the compiler to decide. So no, even in modern compilers those two snippets generally won't end up being compiled to the same result ...

derpirscher
  • 14,418
  • 3
  • 18
  • 35
0

The multiple if else is faster because the conditional checking process will stop on the match condition. For example, you want to find the value of a variable that is actually equal to 2.

var x = 2;

if (x==1) {
} else if (x==2) { // -- stop here because the variable is equal to 2 
} else if (x==3) { // -- unchecked
} else if (x==4) { // -- unchecked
}

Without else, the checking process will be continued.

var x = 2;

if (x==1) {
} 
if (x==2) { // -- match condition 
} 
if (x==3) { // -- keep checking
} 
if (x==4) { // -- keep checking
}

The conditional statement without the else condition is usually used when each of if blocks will return something in a match condition that will stop the execution of the rest statements. For example, you use it in a function that returns a value.

function getValue(x) {
  if (x==1) {
    return 'one';
  } 
  if (x==2) {  
    return 'two';
  } 
  if (x==3) { // -- unreachable if x == 2
    return 'three';
  } 
  if (x==4) { // -- unreachable if x == 2
    return 'four';
  }
}

var x = 2;
var value = getValue(x);
Luki B. Subekti
  • 822
  • 10
  • 9
  • `because the conditional checking process will stop on the match condition` - the OP specifically confirms that and is asking whether the compiler is able to turn one into another. The linked duplicate shows that when the compiler is able to figure it out, then the `else if` is *not* faster, contrary to your claim. – GSerg Jul 21 '21 at 08:58
-1

In this case if else will be faster because if it enters in the first if it will "jump" the rest else cases. With the if statement, this won't happen.

Example of execution with if-else --> I==1
Compares the first if
Executes the first if
End

Example of execution with if --> I==1
Compares the first if
Executes the first if
Compares the second if
Compares the third if
End

As you can see this if else is an optimal solution that only run the same number of comparisons as if when i is equal to the last case

With compilers it would be something similar so be sure to use if else in this kind of situations instead lots of if