I was wondering, if we have if-else condition, then what is computationally more efficient to check: using the equal to operator or the not equal to operator? Is there any difference at all?
E.g., which one of the following is computationally efficient, both cases below will do same thing, but which one is better (if there’s any difference)?
Case1:
if (a == x)
{
// execute Set1 of statements
}
else
{
// execute Set2 of statements
}
Case 2:
if (a != x)
{
// execute Set2 of statements
}
else
{
// execute Set1 of statements
}
Here assumptions are most of the time (say 90% of the cases) a will be equal to x. a and x both are of unsigned integer type.
GCC provides a way to inform the compiler about the likely outcome of an expression:
This built-in evaluates to the value of
expression, but it informs the compiler that the likely result is 1 (truefor Booleans). To use this, you should writeexpressionas clearly as possible (for humans), then set the second parameter to whichever value is most likely to be the result.