If and more, should I include the more likely part?

I was wondering if there is a big difference in performance in languages, whether to use the code more likely in if or in else . Here is an example:

 // x is a random number, or some key code from the user if(!somespecific_keycode) do the general stuff else do specific stuff 

and another solution

 if(somespecific_keycode) do the specific stuff else do general stuff 
+8
c if-statement flow-control
source share
11 answers

They prefer to put them in order, which makes the code clearer, which usually has more likely execution in the first place.

+6
source share

As others have said: in terms of performance, it is best to use your compiler and your hardware (branch prediction, speculative execution) to do the right thing.

If you are really worried that these two do not help you, GCC provides builtin (__builtin_expect) with which you can explicitly indicate the expected branch result.

In terms of code readability, I personally like that the more likely case should be on top.

+7
source share

If you do not have a performance problem, do not worry about it.

If you have a performance problem, try switching them and measure which option is faster, if any.

+3
source share

branch prediction will make one of them more likely, and this will cause a difference in performance if inside the loop. But basically you can ignore this if you are not thinking at assembly level.

+1
source share

The general rule is to first pose a more likely case, which is considered more readable.

+1
source share

This basically doesn't matter, but sometimes it's easier to read and debug if your ifs checks to see if something is true or equal, and else processes it when it isn't.

0
source share

As others have said, this will not make much difference if you do not use it many times (in a loop, for example). In this case, first set the most probable condition, since it will have the earliest opportunity to exit the status check.

This becomes more apparent when you start to have a lot of "else if".

0
source share

Any difference that may occur is more related to the context than inherently to the if-else . Thus, the best thing you can do here is to develop your own tests to detect any difference.

If you are not optimizing an already completed system or software, then I would recommend that you avoid premature optimizations . You may have already heard that they are evil.

0
source share

AFAIK with modern optimizing C compilers, there is no direct connection between how you arrange your if or loops and the actual branch instructions in the generated code. In addition, different CPUs have different branch prediction algorithms.

Thus:

  • Do not optimize until you see the poor performance associated with this code.

  • If you optimize, measure and compare different versions

  • Use realistic data with different characteristics to measure performance

  • Look at the build code generated by your compiler in both cases.

0
source share

This is not necessarily about performance, but I usually go from specific to general to prevent such cases:

 int i = 15; if(i % 3 == 0) System.out.println("fizz"); else if(i % 5 == 0) System.out.println("buzz"); else if(i % 3 == 0 && i % 5 == 0) System.out.println("fizzbuzz"); 

Here the code above will never say "fizzbuzz" because 15 matches the conditions i % 3 == 0 and i % 5 == 0 . If you reorder something more specific:

 int i = 15; if(i % 3 == 0 && i % 5 == 0) System.out.println("fizzbuzz"); else if(i % 3 == 0) System.out.println("fizz"); else if(i % 5 == 0) System.out.println("buzz"); 

Now the above code will reach fizzbuzz before more general conditions stop it

0
source share

All answers have valid scores. Here is another one:

  • Avoid double negatives: if not , then this, otherwise something may confuse the reader. Therefore, for the above example, I would prefer:

     if (somespecific_keycode) { do_the_specific_stuff(); } else { do_general_stuff(); } 
0
source share

All Articles