In the GCC (version 4.8.2) manual, the following is stated:
-ftree-loop-if-convert-stores:
Attempt to also if-convert conditional jumps containing memory writes. This transformation can be unsafe for multi-threaded programs as it transforms conditional memory writes into unconditional memory writes. For example,for (i = 0; i < N; i++) if (cond) A[i] = expr;is transformed to
for (i = 0; i < N; i++) A[i] = cond ? expr : A[i];potentially producing data races.
I wonder, however, if there is a performance gain by using the operator? versus the if statement.
- In the first piece of code,
A[i]is set toexpronly if the condition is met. If it is not met, then the code inside the statement is skipped. - In the second one,
A[i]seems to be written regardless of the condition; the condition only affects the value it is set to.
By using operator?, we are also doing a check; however, we are adding some overhead in the case that the condition is not met. Have I missed something?