0

I know there really won't be a big change but I'm generally curious. Which is faster one or two?

1:

if (foobool)
{
    // FOOBOOL!
}
else
{
    foobool = true;
}

2:

if (foobool)
{
    // FOOBOOL!
}
// else
// {
    foobool = true;
//}

I figure there is more going into making a if else statement than setting a bool to true. However if I don't put the else statement and the bool is already true then it would be setting it twice. But would adding the else statement would also make it slower?

SplatTab
  • 11
  • 3
  • Also sorry for the crappy title its kind of hard to phrase. – SplatTab Apr 01 '22 at 23:41
  • 1
    Does this answer your question? [What does a branchless if in C++ look like?](https://stackoverflow.com/questions/6133322/what-does-a-branchless-if-in-c-look-like) – Sergey Kolesnik Apr 01 '22 at 23:48
  • @SergeyKolesnik well with return its a different story because everything stop there but in this case after the if statement is done it will still continue – SplatTab Apr 01 '22 at 23:53
  • 1
    Is this one time or in a loop? Is foobool volatile and does it have to be synchronized between processors? A lot of questions to answer to save very little if anything. Whatever you are saving , I bet it probably makes less than 1-microsecond difference in your application, so it is not something to worry about. Write something that looks clear to you when you look at it in 6 months or 4 years from now. – Tony BenBrahim Apr 01 '22 at 23:53
  • @TonyBenBrahim Thank you and the second does seem clearer to me so its a double win It is pretty interesting too because just looking at the code it seems not setting it to true twice would be more efficient – SplatTab Apr 01 '22 at 23:57
  • 1
    If we're throwing out small changes, `if (std::exchange(foobool, true))` could potentially be clear depending on the code it's in. Depends whether you want `foobool` to be thrown out of the reader's mind sooner or kept in mind after the body. It also depends what exactly that piece of code is trying to say, e.g., "`foobool` is tested here, but all future tests will pass" or "`foobool` is tested and then reset to its default of `true`". – chris Apr 02 '22 at 00:01
  • Also it is in a loop so it adds up – SplatTab Apr 02 '22 at 00:02
  • 2
    *Also it is in a loop so it adds up* How do you know it "adds up"? After compiling your code at a high level of optimization, you've profiled both versions of the code, obtained detailed performance data, and identified this boolean check as a performance bottleneck? – Andrew Henle Apr 02 '22 at 00:04
  • Why not just throw both in a bench and get a “definitive” answer? – Taekahn Apr 02 '22 at 00:05
  • @Taekahn Would be nice to see an exaple that would not be completely optimized out in a bench :) – Sergey Kolesnik Apr 02 '22 at 00:07
  • 1
    New title `Would setting a value to true if it's not true be more effecient than checking if the value is true then setting it to true` makes no sence. You choose between "reassinging it in else branch" and "reassigning regardless of its previous value" – Sergey Kolesnik Apr 02 '22 at 00:10
  • 1
    https://www.quick-bench.com/q/ImcjS4wOSvThkRce-tcNqqFUBeo Looks like the second one is cheaper – Fatih BAKIR Apr 02 '22 at 00:27
  • @FatihBAKIR I set to C++17 and GCC 11.2 and both versions were the same – Sergey Kolesnik Apr 02 '22 at 00:40
  • Is there code in any of the branches? If not `foobool = true;` (without the conditional) would clearly be the fastest. If there is code there, that code would be relevant to running any benchmarks because pretty much everything impacts the optimiser. – bitmask Apr 02 '22 at 01:10

0 Answers0