10

I was wondering if there is a big performance difference in languages, whether you should put the more likely to be executed code in the if or in the else clause. 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 the other solution

if(somespecific_keycode)
   do the specific stuff
else
   do general stuff
Jonas
  • 121,568
  • 97
  • 310
  • 388
Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75
  • That depends heavily on the compiler, the compiler version, the optimizing options you chose and sometimes just the weather. But in 99.999% of all real-world cases I can think of you will most probably notice no difference. – Doc Brown Aug 01 '12 at 16:41
  • There will be a performance difference... on the scale of **NANOSECONDS!** That won't be noticeable to anyone except superman. – Marlon Aug 01 '12 at 16:48

11 Answers11

10

Prefer to put them in the order that makes the code clearer, which is usually having the more likely to be executed first.

pb2q
  • 58,613
  • 19
  • 146
  • 147
8

As others said: in terms of performance you should best rely on your compiler and your hardware (branch prediction, speculative execution) to do the right thing.

In case you are really concerned that these two don't help you enough, GCC provides a builtin (__builtin_expect) with which you can explicitly indicate the expected outcome of a branch.

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

BjoernD
  • 4,720
  • 27
  • 32
4

Unless you experience a performance problem, don't worry about it.

If you do experience a performance problem, try switching them around and measure which variant is faster, if any of them.

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
2

The common rule is to put more likely case first, it's considered to be more readable.

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
1

branch prediction will cause one of those to be more likely and it will cause a performance difference if inside a loop. But mostly you can ignore that if you are not thinking at assembler level.

Markus Mikkolainen
  • 3,397
  • 18
  • 21
1

This isn't necessarily a performance concern, but I usually go from specific to general to prevent cases like this:

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 above code will never say 'fizzbuzz', because 15 matches both the i % 3 == 0 and i % 5 == 0 conditions. If you re-order into 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 getting stopped by the more general conditions

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
1

All answers have valid points. Here is an additional one:

  • Avoid double negations: if not this, then that, else something tends to be confusing for the reader. Hence for the example given, I would favor:

    if (somespecific_keycode) {
        do_the_specific_stuff();
    } else {
        do_general_stuff();
    }
    
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

It mostly doesn't make a difference but sometimes it is easier to read and debug if your ifs are checking if something is true or equal and the else handles when that isn't the case.

edhedges
  • 2,722
  • 2
  • 28
  • 61
0

As the others have said, it's not going to make a huge difference unless you are using this many many times (in a loop for example). In that case, put the most likely condition first as it will have the earliest opportunity to break out of the condition checking.

It become more apparent when you start having many 'else if's .

Inisheer
  • 20,376
  • 9
  • 50
  • 82
0

Any difference that may arise is more related to the context than inherently with if-else constructions. So the best you can do here is develop your own tests to detect any difference.

Unless you are optimizing an already finished system or software, what I'd recommend you is avoid premature optimizations. Probably you've already heard they are evil.

Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
0

AFAIK with modern optimizing C compilers there is no direct relation between how you organize your if or loop and actual branching instructions in generated code. Moreover different CPUs have different branch prediction algorithms.

Therefore:

  • Don't optimize until you see bad performance related to this code

  • If you do optimize, measure and compare different versions

  • Use realistic data of varied characteristics for performance measurement

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

Tomek Szpakowicz
  • 14,063
  • 3
  • 33
  • 55