Because of this particular additional test, the equivalent code for this is:
for (unsigned i = 0; i < 100000; ++i) { // Primary loop for (unsigned c = 0; c < arraySize; ++c) { if (data[c] >= 128) sum += data[c]; //With this additional test, execution becomes faster if (data[c] < 128) sum += data[c]; } }
becomes the following:
for (unsigned i = 0; i < 100000; ++i) { // Primary loop for (unsigned c = 0; c < arraySize; ++c) { sum += data[c];//because exactly one condition is guaranteed to be //true in each iteration (in your code)! //the equivalent is as if there is no condition at all! } }
therefore it becomes faster.
Due to the unusual additional test and identical body, the compiler is able to optimize the code by removing the if conditions. When you have one if , the compiler cannot do this.
Try writing this:
sum -= data[c]; //the body is not identical anymore!
in one of the conditions if . I am sure that the compiler will not be able to optimize the code. Now it should publish a slower machine code.
Please note that the outer loop can be completely eliminated, although it does not depend heavily on the additional test:
// Primary loop for (unsigned c = 0; c < arraySize; ++c) { sum += 100000 * data[c]; }
or that:
// Primary loop for (unsigned c = 0; c < arraySize; ++c) { sum += data[c]; } sum = 100000 * sum; //multiple once!
Nawaz source share