Optimization of complex conditions
When it comes to a program with many conditional statements, usually if()
statements, the effectiveness of the branch prediction often determines the overall performance. If the branch is predicted accurately, it has almost no cost. If the branch is mispredicted half the time, it can be as expensive as ten or more regular arithmetic instructions.
It is very important to understand that the hardware branch prediction is based on the conditional instructions executed by the processor. As such, the processor's understanding of what a condition is can be different from our understanding. The following example helps to drive this point home, with force:
02_branch.C
std::vector<unsigned long> v1(N), v2(N); std::vector<int> c1(N), c2(N); for (size_t i = 0; i < N; ++i) { v1...