C ++: set bool value only if it is not set

I have code in my C ++ application that usually does this:

bool myFlag = false; while (/*some finite condition unrelated to myFlag*/) { if (...) { // statements, unrelated to myFlag } else { // set myFlag to true, perhaps only if it was false before? } } if (myFlag) { // Do something... } 

The question I have relates to the else expression of my code. Basically, my loop can set the value of myFlag from false to true, based on a certain condition that is not satisfied. The flag will never be set from true to false. I would like to know which wording makes more sense in terms of performance, and perhaps if this problem is not really a problem due to compiler optimization.

 myFlag = true; 

OR

 if (!myFlag) myFlag = true; 

Normally, I would choose the former because it requires less writing code. Nevertheless, I began to think that perhaps this is due to unnecessary writing in memory, and therefore the latter will prevent unnecessary writing if myFlag was already true. But, using the latter, it takes more time because there is a conditional statement and, therefore, to compile the code using additional instructions?

Or maybe I thought too much about it ...


UPDATE 1

Just to clarify a bit ... the goal of my last case is not to write to the memory if the variable was already true. Thus, only write to memory if the variable is false.

+7
source share
6 answers

You are almost certainly best off using myFlag = true; .

About the best you can hope for from if (!myFlag) myFlag = true; is that the compiler will notice that if does not matter and optimizes it. In particular, the if should read the current value of myFlag . If this value is not yet specified in the cache, this means that the command will stop waiting for data to be read from memory.

In contrast, if you just write (without testing first), the value can be written to the write queue, and then several more commands can be executed immediately. You won't get a stall until / if you read the value of myFlag (and assuming that it reads fast enough after writing, it will probably still be in the cache, so the stop will be minimal).

+9
source

Do you understand that the check is correct? If you blindly set it to true and it has not been installed, you install it. If it was already true , then there are no changes, and you are not , so you can effectively implement it as:

 myFlag = true; 

As for potential optimizations, in order to be able to verify, the value should be in the cache, so most of the cost has already been paid. On the other hand, a branch (if the compiler does not optimize if , which will be the most), this may have a greater impact on performance.

+2
source

processor cycle wise, prefer myFlag = true; Think about it: even if the compiler does not do the optimization (in fact, this is not so), just the installation requires one asm operator, and through if at least 1 asm is required.

So, just go to the destination.

And more importantly, do not try to make hypotheses on such low-level details, specific compiler optimizations may completely contradict intuition.

+1
source

Most likely, you changed your mind about the problem, as already mentioned, so let me do the same. The following may be faster if you can allow double claims not related to myFlag . In fact, you can get rid of myFlag . Ok, here we go:

 while (/*some finite condition*/) { if (...) { // statements } else { while (/*some finite condition*/) { if (...) { // statements, repeated } } // Do something (as if myFlag was true in the OPs example) break; } } 

As with any performance optimization: measurement, measurement, measurement!

0
source

This is a specific architecture if if (!myFlag) myFlag = true; It will take more time to complete than a simple myFlag = true; even without any optimization. There are architectures (e.g. https://developer.qualcomm.com/hexagon-processor ) where both operators will only execute one loop to execute.

The only way to find out on your machine is to measure.

In any case, myFlag = true will always be faster or have the same runtime as if (!myFlag) myFlag = true;

0
source

This question gave me a headache, so I just tested it myself with the following code (C #):

  System.Diagnostics.Stopwatch time = new System.Diagnostics.Stopwatch(); int i = 0; int j = 1; time.Start(); if (i != 0) i = 0; time.Stop(); Console.WriteLine("compare + set - {0} ticks", time.ElapsedTicks); time.Reset(); time.Start(); if (j != 0) j = 0; time.Stop(); Console.WriteLine("compare - {0} ticks", time.ElapsedTicks); time.Reset(); time.Start(); i = 0; time.Stop(); Console.WriteLine("set - {0} ticks", time.ElapsedTicks); Console.ReadLine(); 

result:

compare + set - 1 tick

compare - 1 ticks

set - 0 ticks

while the time used to set the value is certainly not zero, it shows that even a single request takes more time than just setting the variable.

0
source

All Articles