| 1. |
What Is Loop Unrolling? |
|
Answer» Small loops can be unrolled for higher performance, with the disadvantage of increased codesize. When a LOOP is unrolled, a loop counter needs to be updated LESS often and fewer branches are executed. If the loop iterates only a few times, it can be fully unrolled, so that the loop overhead completely disappears. int CountBitOne(uint n) { int bits = 0; while (n != 0) { if (n & 1) bits++; n >> = 1; } return bits; } int CountBitOne(uint n) { int bits = 0; while (n != 0) { if (n & 1) bits++; if (n & 2) bits++; if (n & 4) bits++; if (n & 8) bits++; n >> = 4; } return bits; }Small loops can be unrolled for higher performance, with the disadvantage of increased codesize. When a loop is unrolled, a loop counter needs to be updated less often and fewer branches are executed. If the loop iterates only a few times, it can be fully unrolled, so that the loop overhead completely disappears. |
|