In order to optimize for execution speed, it is recommended to avoid using if-conditions inside a loop. Imagine there is a case where loop unswitching is not possible, but information (or an estimate) about the frequency of the conditions is available.
Does the order of conditions in an if-else-statement influence execution speed?
Assume I now (or estimate) condition A
occurs 80%, B
occurs 15% and C
only 5% and the times to calculate the conditions is equal. Would it the better to write the loop like this or does the order makes no difference in execution time?
for (i in N){
if (A(i))
foo(i)
else if (B(i))
bar(i)
else
foobar(i)
}
Are there any best practices regarding order of conditions? Is it language dependent? What if the time to evaluate conditions is not equal? Would it be the best to order the conditions from cheapest to most expensive in that case?