Firstly, processors have a feature called branch prediction . After several loop cycles, the processor will be able to notice that your if always goes in one direction. (He may even notice regular patterns, for example true false true false .) Then he will speculatively execute this branch and as long as he is able to correctly predict, the additional cost of the if is largely eliminated. If you think that the user is more likely to select true rather than false , you can even report this to the gcc (gcc-specific) compiler .
However, in one of your comments, you mentioned that you have a "more complex sequence of bools". I think it is possible that the processor does not have memory to match the pattern with all these transitions - by the time it returns to the first if , knowing which path that jumped is offset from its Memory. But we could help here ...
The compiler has the ability to convert loops and if-statements to what it considers more optimal. For instance. it could turn your code into a form given by schnaader. This is called loop unswitching . You can help him by doing Profiled Optimization (PGO) , letting the compiler know where the hotspots are, (Note: In gcc, -funswitch-loops is only enabled with -O3 .)
You need to profile your code at the instruction level ( VTune will be a good tool for this) to see if if statements are really a bottleneck. If they really are, and if, looking at the generated assembly, you think that the compiler was wrong, despite the PGO, you can try to execute the if-statement yourself. Perhaps the boilerplate code will make it more convenient:
template<bool B> void innerLoop() { for (int i=0; i<10000; i++) { if (B) { // some stuff.. } else { // some other stuff.. } } } if (user_set_flag) innerLoop<true>(); else innerLoop<false>();
source share