C# evaluate for a value greater than 0 given 3 variables -


is more efficient use: if ( a+b+c > 0) or if ( a>0 || b>0|| c>0) or there better way either of these?

these 2 expressions not equivalent: first expression false a=1, b=0, c=-1, while second true.

the first expression require 2 additions, comparison zero, , branch, while second expression require 3 comparisons 0 , 3 branches, because || operator short-circuiting. in end, difference going undetectably small.

the case when second expression win when a, b , c represent expensive computations:

if (expensivecomputationa() > 0 ||  expensivecomputationb() > 0 ||  expensivecomputationc() > 0) {     ... } 

since computation above stop after first success, resultant code faster result of short-circuiting expensive branches.


Popular posts from this blog