November 1999
Intermediate to advanced
336 pages
6h 29m
English
As previously discussed, branching can be expensive in performance terms. Frequently, branching cannot be avoided, but sometimes branching is used in place of calculation. This can be a critical performance mistake. Take, for example, the following code sequence:
const int X_MAX = 16;
...
++x;
if (x >= X_MAX) {
x = 0;
}
It is really just a bit mask operation and can just as easily be done with the following code sequence:
const int X_MASK = 15; ... x = (x + 1) & X_MASK;
In the first case there is a load, an increment, a store, a conditional test, and another store. Even on a machine with a short pipeline, the conditional test will cost at least three cycles, and thus the effective cost of the conditional ...
Read now
Unlock full access