开发者

Would it make sense to arrange the boolean flags in certain order for better performance

boolean a = m1();
boolean b = m2();
boolean c = m3();

Currently my code looks l开发者_开发百科ike this and mostly the condition for "c" is bound to be true

if (a || b || c)

Would it make sense to re-write the code as if (c || a || b), so that the if condition can be quickly evaluated.


Yes that would make sense, but evaluating just booleans is negligible. You'd probably be better inlining the method calls, though so you only execute the code which is neccessary:

if (m3() || m1() || m2())


Yes. If c is true in most cases, then the expression will be evaluated faster in those most cases.

And this is a language feature. In JLS 15.24 "Conditional-Or Operator ||" we read:

At run time, the left-hand operand expression is evaluated first; [...] if the resulting value is true, the value of the conditional-or expression is true and the right-hand operand expression is not evaluated.


depends on what m1() or m2() is doing. DB-Connection? or getting a variable.


Doesn't matter - finish your app. If it runs slow, find the slowest part. Optimize that. If your app still runs slow, repeat the process.


The order does not matter, since you are always calling each of the functions.

If you were to do it like this:

if (m3() || m1() || m2()) {}

the performance could be better.

Edited: correct method names


Taking advantage of conditional operator short-circuit is a good practice but if you evalueate the condition before you have not real gain to use short circuit. If you write

 if(m3() || m1() || m3())

the short circuit avoid calling other function.


The thing is if you write the code the way you described it:

boolean a = m1();
boolean b = m2();
boolean c = m3();
..
if( a || b || c ) ...

the order of a, b and c in the if statement is rather irrelevant because evaluating a variable will be very very fast -- so whether it evaluates a and b and c it won't matter that much: each of the 3 functions have already been called and the values stored (in a, b and c). However, if you re-write your code to include the actual function calls then it starts to matter because you won't have to execute/evaluate each of the m..() functions each time:

if( m1() || m2() || m3() ) ...

at this point you can indeed talk about the order of evaluation and it might make sense to have the function which takes less time to execute first and the one which takes most time to execute last -- so if the "fast" function returns false then you don't evaluate the one that takes most time/CPU/etc.


It would make sence in just a theoretical way. Really you won't notice the difference. Don't bither yourself with such microoptimisations.


Java's logical OR || and AND && operators are short-circuit operators: as soon as the result of the expression is clear, the rest of the expression will not be evaluated. So if you write

if (c || a || b)

and c is true, then a || b will not be evaluated, because the result of the whole expression is going to be true anyway.

So there is a theoretical small performance benefit, but I agree with other answerers that in general it doesn't make much sense to worry about micro-optimizations like this; most likely you will not notice any real performance benefit when running your program.


Would it make sense to re-write the code as if (c || a || b), so that the if condition can be quickly evaluated.

Yes.


If you do not want to evaluate all three methods, then inlining(As others said) is the best solution.


If have literally if (a || b || c) it might be faster to use

if (a | b | c)

As using || involves some branching which can be expensive, however | is just an arithmetic operation. Using || makes sense you have a non-trivial operation or you want to avoid a side effect like a NullPointerException.

If you have the following but each is about the same cost, its worth considering which one is most likely to be true

if( mostLikelyToBeTrue() || m2() || leastLikelyToBeTrue())

OR

if( mostLikelyToBeFalse() && m2() && leastLikelyToBeFalse())
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜