1

I have a rather tight loop with the following check to see if balance had ever been positive:

balance_null = True

while (crazy_loop()):
    ...
    if 0.0 < balance:
       balance_null = False

In no place would balance_null ever be set back to True. Would the code be more efficient if I were to check the state of balance_null before setting it?

balance_null = True

while (crazy_loop()):
    ...
    if 0.0 < balance and `balance_null`==True:
       balance_null = False

The code example is in Python, but I am actually interested in the general case, which is why I'm not simply benchmarking it myself. What is considered a best practice, and what are the exceptions to the rule?

4
  • 2
    You should profile it. If I were to guess, checking the boolean first before setting it probably slows the code down in the general case, though I doubt there's enough of a performance difference to matter. But measure, don't guess. There's no best practice here; not everything has to be a "best practice," whatever that is. Do what makes sense for your particular application. Feb 1, 2015 at 17:25
  • I'm sure if I profile it in Python that won't be relevant for C++. I'm looking for the general case best practice. The question hinges on what is more expensive: a boolean lookup or a boolean assignment. As I cannot test every case I'd like to know what those who have more experience than I do, usually do.
    – dotancohen
    Feb 1, 2015 at 17:27
  • 1
    That sort of check should perform consistently the same no matter where you put it. Profile it, and see what it does. Personally, I wouldn't bother with the check; there are almost certainly other parts of your code that are orders of magnitude slower than this, so it isn't going to matter anyway. If you really want a "best practice," nobody ever checks a boolean variable to see if it's true before setting it to false. Feb 1, 2015 at 17:33
  • Branch prediction Feb 1, 2015 at 20:13

3 Answers 3

1

You shouldn't check it -- not because it is more expensive to do so (given todays cpu's that is by no means certain), but because it is conceptually unnecessary. Adding it makes the code harder to reason about.

All code should serve a purpose, when reading the code we not only interpret it, we also try to extract that purpose -- to understand both what it is doing, and why.

Code which serves no purpose is thus a hinderance. It can cause considerable delays as a purpose is sought -- and of course not found.

If, in a specific case, you were to profile the code, find a significant performance impact, then you could add the check, with an appropriate comment (said comment to include compiler and version tested).

1
  • Thank you, I do believe that you are right. No premature optimization, so I should add the check iff I see that setting the boolean is expensive.
    – dotancohen
    Feb 1, 2015 at 18:35
4

Setting a boolean variable is a vanishingly small cost in most programming languages. Don't check it first to see if it's set; just set it.

1
  • 2
    For completeness, conditionally jumping is ery expensive. Feb 1, 2015 at 19:50
1

If setting the boolean is the only reason for the loop then you may just as well break out of it when setting the boolean:

balance_null = True

while (crazy_loop()):
    ...
    if 0.0 < balance:
       balance_null = False
       break;

or extract the loop to a function and use early return.

2
  • Setting the boolean is not the only reason for the loop, the ... indicate other code which is not there. By the way, didn't I just see you on Aviation.SE? :)
    – dotancohen
    Feb 1, 2015 at 18:33
  • @dotancohen I'm around on several SEs :) Feb 1, 2015 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.