Evaluating each of the conditions requires several memory accesses
Why don't you avoid short-circuit evaluation within individual conditions, but let it happen for the or-ing of conditions?
Using operators that are not short-circuit for built-in types
Exactly how you achieve the former depends on the nature of those conditions (i.e. condition1
, condition2
in your code) - given you show nothing about them I can only talk in generalities: where they internally contain short-circuit operators, instead convert the boolean value to an integer representation and use e.g. bitwise-OR (or even '+' or '*' if it reads better and works in your particular usage). Bitwise operators are generally safer as they're lower precedence though - only have to be careful if your conditions already include bitwise operators.
To illustrate:
OLD: return (a > 4 && b == 2 && c < a) || // condition1
(a == 3 && b != 2 && c == -a); // condition2
NEW: return (a > 4 & b == 2 & c < a) ||
(a == 3 & b != 2 & c == -a);
Also be careful if you used implicit conversion of numbers/pointers to bool
before... you want to normalise them to bool
s so their least-significant bits reflect their boolean significance:
OLD: return my_int && my_point && !my_double;
NEW: return bool(my_int) & bool(my_point) & !my_double; // ! normalises before bitwise-&
You might also want to benchmark with...
bool condition1 = a > 4 & b == 2 & c < a;
bool condition2 = a == 3 & b != 2 & c == -a;
return condition1 || condition2;
...which might be faster - possibly only in the overall "return false" case and perhaps when the last conditionN or two are the deciding factor in a "return true".
User-defined operators avoid short-circuit evaluation
Seperately, short-circuit evaluation is disabled for objects with overloaded logical operators, which provides another avenue for you to do your checks using the existing notation, but you'll have to change or enhance your data types.
Thoughts
More generally, you'll only benefit from this if you've a large number of assertions combined in each individual condition - more so if the function tends to run through to return false.
"AProgrammer" makes a great point too - with speculative execution available on modern CPUs, the CPU may already be ahead of the ordering implied by short-circuit evaluation (in some special mode than either avoids or suppresses any memory faults from dereferencing invalid pointers, divides by 0 etc). So, it's possible that the entire attempt at optimisation may prove pointless or even counterproductive. Benchmarking of all alternatives is required.