Reputation: 2730
I have a simple performance doubt. When I have to check several variables, which of these two options has more performance?
return (a >= 0 && b >= 0 && c >= 0 && d >= 0);
or
return !(a < 0 || b < 0 || c < 0 || d < 0);
I ask this because I suppose that the || operator stops when it founds a TRUE condition, but the && operator has to compare with the whole conditions.
So... which is better?
Upvotes: 1
Views: 806
Reputation: 5730
Your first example will stop when the first false condition is found and your second will stop when the first true condition is found. Given that the second has a series of comparisons each of which will be true when the first is false, the two return statements will stop after the same number of comparisons. So, unless the compiler does something unusual with the two sets of comparisons, they should execute in similar time.
Upvotes: 3