What is the difference when setting the maximum possible true state in an if, else-if or else

Is there any difference to put the most likely condition in an if, else-if or condition

Example:

int[] a = {2,4,6,9,10,0,30,0,31,66}
int firstCase = 0, secondCase = 0, thirdCase = 0;
for( int i=0;i<10;i++ ){
    int m = a[i] % 5;
    if(m < 3) {
        firstCase++;
    } else if(m == 3) {
        secondCase++;
    } else {
        thirdCase++;
    }
}

      

What is the difference in runtime with input

int[] a = {3,6,8,7,0,0,0,0,0,0}

      

+3


source to share


7 replies


Is there any other way to put the most possible true condition in an if, else-if or condition

Actually, the answer from Java is "it depends".

You see, when you run Java code, the JVM is started using the interpreter while collecting statistics. One of the statistics that can be recorded is which path in a branch instruction is most often taken. These statistics can then be used by the JIT compiler to influence code reordering, where this does not change the compiled semantics of the code.

So, if you were to execute your code with two different sets of data (ie, "mostly zero" and "mostly nonzero"), it is possible that the JIT compiler will compile the code differently.

Is it possible for this optimization to depend on whether he can figure out what the reordering is indeed. For example, is it possible to infer that the conditions being tested are mutually exclusive?




So how does this affect complexity? Well ... let's do the sums for your simplified example, assuming the JIT compiler doesn't do anything "smart". And suppose we are dealing with more than just arrays of length 10 (which makes the discussion a tricky one).

Consider this:

  • For each zero, the loop performs one test and one step - let's say 2 operations.

  • For each non-zero element, the loop performs two tests and one increment - let's say 3 operations.

So, this is about 2 * N operations for N elements, when all are zero versus 3 * N operations, and all are nonzero. But both are O(N)

... so the complexity of Big O is not affected.

(OK, I left some stuff ... but you get the image. One of the cases will be faster, but the complexity will not be affected.)

+6


source


There's a bit more to it than you're told.

  • 'if' versus 'else': If the condition and its reversal are not equally probable, you should handle the more probable condition in the else block, not in the if block. The "if" block requires a conditional branch, which is not accepted, and a final branch around the "else" block; the "else" block requires a conditional branch to be taken and no final branch at all.

  • 'if' versus 'else if' versus 'else': Obviously you should handle the most common case in the if block to avoid the second test. The same considerations as in (1) dictate that the more general case between the final "else if" and the final "else" should be handled in the last "else" block.



Having said all that, unless the tests are non-trivial or the content of all of these blocks is completely trivial, it is unlikely that any of them will make a noticeable difference.

+4


source


It makes no difference if you only have if-else

, since the condition will always evaluate and it doesn't matter if it is almost always true or false. However, if you have if

in part else

( else if

), it is much better to put the largest possible true condition in the first one if

. So most of the time you won't need to evaluate state internally else

, increasing performance.

+2


source


If most of the conditions are true, if then the execution time will be shorter. Because in the first if condition this is only satisfied.

If most of the conditions are true in the if-else, then the execution time will be less than the last scenario and more than the first scenario.

If most of the conditions are true otherwise, the execution time will be longer. Because it checks the first 2 conditions.

+2


source


Sure.

if ... else if ...

the checks are in the order in which they were encoded. So, if you put the maximum possible condition at the end of this queue check condition, this code will run a little slower.

But it all depends on how these conditions are constructed (how complex they are).

+2


source


The most likely condition should go to an if, and then an if else, and so on.

+1


source


It is good to write the most common condition at the very first level, so that if this condition is true or false, it will be processed first in less time.

If you put the most common condition in the middle (else..if) or in the last one (else), it will take a while to get to that condition, because it needs to check each condition.

+1


source







All Articles