-1

I have been programming specifically on boolean values where I want to know whether changing bool values or comparing boolean values provide better performance in terms of execution speed. Please enlighten me on this one. I have written two instances on changing and comparing boolean values.

1.

bool b = false;

while (true) {
    b = true;
}
bool b = false;

while (true) {
    if (b == false)
        b = true;
}

which one provides better performance?

L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28
gmmh0609
  • 1
  • 1
  • 13
    I assume the compiler would optimize away everything except the infinite loop. Have you checked the assembly? – Stephen Newell Oct 19 '20 at 04:59
  • https://godbolt.org/ – Martheen Oct 19 '20 at 05:02
  • 3
    You should always measure. And even that's only required if you run into performance issues. Rather focus on writing maintainable readable tested code. – JHBonarius Oct 19 '20 at 05:05
  • 1
    Also note that the two do the same thing here, that is set `b` to `true` (and the compiler knows this and just removes everything). But if we assume, a real program, you would maybe consider `b = SomeLongAndComplexCondition();`. Then the two are different, in the first version `b` can change on each iteration, in the second version `b` can only change to `true` once and then never back to `false`. – Lukas-T Oct 19 '20 at 05:20
  • The code you write is only a description of what the program should do. The compiler is then free to do anything with the code, as long as the final results are correct. The final product produced by the compiler and optimizer may not look anything like what your code states. Read up on the [as-if](https://en.cppreference.com/w/cpp/language/as_if) rule for C++. – PaulMcKenzie Oct 19 '20 at 05:51
  • 1
    This is tagged both “c” and “c++”. Which is it? – Ry- Oct 19 '20 at 06:10
  • 2
    Assuming that your actual question is not "which of these loops is faster", but "does it make sense to test whether a boolean already has the desired value and not assign to it if it does", a branch is far more expensive than an assignment to a primitive in a modern CPU. – molbdnilo Oct 19 '20 at 08:02

1 Answers1

1

C++ operates on an abstract machine. That abstract machine’s behaviour, when defined, is translated into actual machine code.

Your code runs into a few problems.

The biggest is that all loops in C++ are guaranteed to either perform an I/O-like operation, synchronize, or end.

The second problem is that, barring synchronization, intermediate states are undetectable.

So both of your program segments can be compiled down to:

bool b = true;

by a compliant C++ compiler.

(Now, as your loops never actually terminate, compilers are free to do even stranger things.)

Optimization in C++ depends on writing code the compiler can understand to optimize. The steps you write in C++ cannot be assumed to correspond 1:1 with generated machine code. That error is common and leads to both bugs in C++ code and optimization failures.

Basically, you are asking how tall a cake dough recipe is. It is a category error, where you know cake has a height, and are mistaking a recipe for cake dough as having the properties of a cake.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524