|
|
|
|
|
by Someone
4149 days ago
|
|
when choosing between A and B
and B and A
You want to compare expected execution times, which are: E(A) + p(A) E(B)
(E(X) =expected execution time of X, p(X) = probability that X evaluates to true) and E(B) + p(B) E(A)
The first is larger if (1 - p(B)) E(A) > (1 - p(A)) E(B)
End result is that it may be better to put the expensive call first, if the probability of the cheap call being true is much higher than that of the expensive call.[Feel free to extend this model by adding the time needed to make or not make the branch around the second part] In practice, if one is a function call and the other is not, put the function call last, then, if performance is inadequate, measure. |
|