C++: Set bool value only if not set

后端 未结 6 2178
小鲜肉
小鲜肉 2020-12-17 22:19

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

bool myFlag = false;
while (/*some finite condition unrelated to myFlag*/) {
    if (...) {
               


        
6条回答
  •  心在旅途
    2020-12-17 23:00

    It is architecture specific whether if (!myFlag) myFlag = true; will take more time to execute than the simple myFlag = true; even without any optimization. There are architectures (e.g., https://developer.qualcomm.com/hexagon-processor) where both statements will take only one cycle each to execute.

    The only way to figure out on your machine would be by measurement.

    In any case myFlag = true will always be faster or have same execution time as if (!myFlag) myFlag = true;

提交回复
热议问题