1

If I have some code with up to 9 if statements in some of my methods and all most all of them will intrinsically only meet one of the if requirements, would using else if speed my code up?

For example, if I had this

if (x == 1){
    do something;
}
if (x == 2){
    do something;
}
if (x == 3){
    do something;
}
if (x == 4){
    do something;
}

Would changing it to this speed up the code?

if (x == 1){
    do something;
}
else if (x == 2){
    do something;
}
else if (x == 3){
    do something;
}
else if (x == 4){
    do something;
}

In my actual code the if statements are evaluating more complex stuff than integers.

ChrisWilson4
  • 195
  • 4
  • 23
  • What exactly do your if statements look like? – Bruno Reis Aug 14 '13 at 09:22
  • 3
    The other option would be switch case – SpringLearner Aug 14 '13 at 09:22
  • I agree with javaBeginner. Why don´t you use switch case? That would fit perfectly. This could be interesting for you http://stackoverflow.com/questions/2086529/what-is-the-relative-performance-difference-of-if-else-versus-switch-statement-i – Christian Lendel Aug 14 '13 at 09:23
  • @javaBeginner performance-wise, a switch would translate to a series of if / else if / else so it would make no difference. – assylias Aug 14 '13 at 09:23
  • @assylias sorry but i could not understand your comment properly. A brief explanation is expected – SpringLearner Aug 14 '13 at 09:25
  • @assylias Not necessarily. Bytecode has a couple of switch commands, suitable for different situations. – Patricia Shanahan Aug 14 '13 at 09:26
  • @PatriciaShanahan For the code in the question it would be. Switch starts using lookup table when there are 18 or more consecutive values IIRC (the bytecode might use a loopkup switch in that case but the JIT would translate it into if/elseif anyway). – assylias Aug 14 '13 at 09:27
  • @javaBeginner the code generated by the JVM for the code in the question would probably not change if a switch was used instead. However I agree that readability would be better. – assylias Aug 14 '13 at 09:28

8 Answers8

6

Probably. It depends on whether the compiler can detect that your conditions are mutually exclusive and optimize based on that. In the simple integer case, it probably can; in more complicated cases, it can't.

But it's not just about the compiler. It's also about the reader. Using else if documents to the reader that the cases are mutually exclusive, and that's a good thing.

Another option is to return from the function at the end of each if block.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
3

It might speed up your code ... or it might make no difference to the performance. It depends on the complexity of the actual tests, and whether the JIT compiler can optimize the two versions to equivalent native code.

But more importantly, adding the else's will make the intent of your code clearer ... assuming that these are really disjoint alternatives.

So for that reason, I'd recommend adding the else's irrespective of the performance.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

No if the last else is getting executed and yes if any intermediate condition is found to be true.

But in general using if-else statement makes more sense if conditions are mutually exclusive. Other option is using switch statement.

After all (java) interpreter works that way. Scans line by line of intermediate bytecode... converts to machine code and executes it(Assuming we are not considering JIT compiler here). So using if-else would be more beneficial than using multiple if statements.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • First, the Java interpreter doesn't work that way. The Java interpreter takes already compiled (and possibly optimized) bytecode and executes it. And not considering the JIT in Java when the question is performance is completely removed from reality and therefore useless. – Sebastian Redl Aug 14 '13 at 09:28
  • Yes it takes intermediate byte codes but JIT performs further optimizations. – Aniket Thakur Aug 14 '13 at 09:30
2

Definitely else part is better performance because , For If case even though the comparison is done it will go ahead with the checks but else part is mutual exclusive hence does not compare further. Below code if you run you can clearly understand. the time is in micro seconds.

public class Test {

public static void main(String[] args) {

    ifCase(5);
    ifElseCase(5);
}

private static void ifCase(int x) {

    long start = System.nanoTime();
    if (x == 1) {
        System.out.println(x);
    }
    if (x == 2) {
        System.out.println(x);
    }
    if (x == 3) {
        System.out.println(x);
    }
    if (x == 4) {
        System.out.println(x);
    }
    if (x == 5) {
        System.out.println(x);
    }
    if (x == 6) {
        System.out.println(x);
    }
    if (x == 7) {
        System.out.println(x);
    }
    if (x == 8) {
        System.out.println(x);
    }
    if (x == 1) {
        System.out.println(x);
    }
    if (x == 2) {
        System.out.println(x);
    }
    if (x == 3) {
        System.out.println(x);
    }
    if (x == 4) {
        System.out.println(x);
    }
    if (x == 5) {
        System.out.println(x);
    }
    if (x == 6) {
        System.out.println(x);
    }
    if (x == 7) {
        System.out.println(x);
    }
    if (x == 8) {
        System.out.println(x);
    }
    if (x == 1) {
        System.out.println(x);
    }
    if (x == 2) {
        System.out.println(x);
    }
    if (x == 3) {
        System.out.println(x);
    }
    if (x == 4) {
        System.out.println(x);
    }
    if (x == 5) {
        System.out.println(x);
    }
    if (x == 6) {
        System.out.println(x);
    }
    if (x == 7) {
        System.out.println(x);
    }
    if (x == 8) {
        System.out.println(x);
    }
    if (x == 1) {
        System.out.println(x);
    }
    if (x == 2) {
        System.out.println(x);
    }
    if (x == 3) {
        System.out.println(x);
    }
    if (x == 4) {
        System.out.println(x);
    }
    if (x == 5) {
        System.out.println(x);
    }
    if (x == 6) {
        System.out.println(x);
    }
    if (x == 7) {
        System.out.println(x);
    }
    if (x == 8) {
        System.out.println(x);
    }

    long end = System.nanoTime();

    System.out.println("time " + (end - start) / 1000);

}

private static void ifElseCase(int x) {
    long start = System.nanoTime();
    if (x == 1) {
        System.out.println(x);
    } else if (x == 2) {
        System.out.println(x);
    } else if (x == 3) {
        System.out.println(x);
    } else if (x == 4) {
        System.out.println(x);
    } else if (x == 5) {
        System.out.println(x);
    } else if (x == 6) {
        System.out.println(x);
    } else if (x == 7) {
        System.out.println(x);
    } else if (x == 8) {
        System.out.println(x);
    } else if (x == 1) {
        System.out.println(x);
    } else if (x == 2) {
        System.out.println(x);
    } else if (x == 3) {
        System.out.println(x);
    } else if (x == 4) {
        System.out.println(x);
    } else if (x == 5) {
        System.out.println(x);
    } else if (x == 6) {
        System.out.println(x);
    } else if (x == 7) {
        System.out.println(x);
    } else if (x == 8) {
        System.out.println(x);
    } else if (x == 1) {
        System.out.println(x);
    } else if (x == 2) {
        System.out.println(x);
    } else if (x == 3) {
        System.out.println(x);
    } else if (x == 4) {
        System.out.println(x);
    } else if (x == 5) {
        System.out.println(x);
    } else if (x == 6) {
        System.out.println(x);
    } else if (x == 7) {
        System.out.println(x);
    } else if (x == 8) {
        System.out.println(x);
    } else if (x == 1) {
        System.out.println(x);
    } else if (x == 2) {
        System.out.println(x);
    } else if (x == 3) {
        System.out.println(x);
    } else if (x == 4) {
        System.out.println(x);
    } else if (x == 5) {
        System.out.println(x);
    } else if (x == 6) {
        System.out.println(x);
    } else if (x == 7) {
        System.out.println(x);
    } else if (x == 8) {
        System.out.println(x);
    }
    long end = System.nanoTime();

    System.out.println("time " + (end - start) / 1000);
}

}

chaosguru
  • 1,933
  • 4
  • 30
  • 44
  • Wow, that is an excellent example! What a significant difference even when comparing integers. On my system it takes the ifs 180 and the if elses 16 milliseconds. Plus you showed me how I could of answered my question in code, I already gave the answer to other user though. – ChrisWilson4 Aug 14 '13 at 09:55
  • No problem :-) Happy that it could help you. – chaosguru Aug 16 '13 at 04:33
1

Using a switch would even more efficient if you're comparing numbers, but yes, using else already speeds things up.

Think about it: in your first example, every comparison is done, even if one has already been hit. In your second example, the machine only does the comparisons until one is true, the remaining comparisons are not executed any more.

When more expensive comparisons are done the effect can be dramatic. I once (~2003) "optimized" a parser in an industrial robot by adding the missing elses. It speed up parsing the configuration file from over 10 seconds to under one second (and even that "solution" is very inefficient but that's another story).

DarkDust
  • 90,870
  • 19
  • 190
  • 224
0

else executes only when if does not, so surely it will speed up. But else means do this in case if does not happen.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Yes. In the else-case, when one condition is found to be true, the other will not be evaluated. If you place them in likliness order, it will probably give you significant impact.

Without else, al the conditions will be evaluated every time.

Bex
  • 2,905
  • 2
  • 33
  • 36
0

It really depends, in the first case where you have all if's, all if's will be gone through by the jvm

where as in the second case, if the condition satisfy with in the first if, you are lucky and the run will be very fast comparatively. else this may take longer than the normal if's if it matches the last "if" in "else if"

Syed Siraj Uddin
  • 583
  • 1
  • 5
  • 13