-5

What is faster in c++?

bool b1;

  1. if(b1 == true) or if(b1) ?

  2. if(b1 == false) or if(!b1) ?

kolus
  • 59
  • 1
  • 2
  • 5
    What did your benchmarks say? What does the assembly say? This is very easy to verify by just compiling, testing and viewing disassembly – MatthiasB Sep 25 '14 at 11:54
  • 1
    Both have undefined behaviour since the variable is uninitialised. But any compiler should produce identical code in both cases. – Mike Seymour Sep 25 '14 at 11:54
  • 3
    That's pretty much useless micro-optimization. – Halim Qarroum Sep 25 '14 at 11:54
  • 5
    This question appears to be off-topic because it is about pre-mature nano-optimization that would be better answered with an experiment. – duffymo Sep 25 '14 at 11:57
  • 2
    From just a style point of view if(b1 == true) is horrible. There's less to type/read/get wrong - and it's more idiomatic - to use the latter. –  Sep 25 '14 at 11:57
  • 4
    Ignore the other comments: `if (b1)` is known to be 11% faster than `if (b1 == true)`, except on Wednesdays, when it is 17% slower, unless you're compiling with `g++ -fallow-wednesday-specific-optimisations`, in which case it's 23% faster. – Paul R Sep 25 '14 at 12:02
  • 1
    kolus is new to stackoverflow.may be this is the first question of him.so instead of downvoting please tell him how to use this site and how to ask question or put the reason to downvote.so only next time he didn't do that mistake – Selva Sep 25 '14 at 12:03
  • @kolus While the unwelcome-wagon may have downvoted you, there are a few upvotes to compensate...and upvotes do more good than damage. Your question is not **that** bad, but would be improved if (for instance) you had submitted your question with some data showing your testing which might suggest "what you have tried" in terms of investigation that could lead you to believe that they would be different. It is "intuitively obvious" to many that these would be equivalent in most compilers, but see my answer for the *general* concern which might defy your intuition. – HostileFork says dont trust SE Sep 26 '14 at 03:05

3 Answers3

4

The C++11 language specification is a document (see its latest draft n3337) in English which don't talk about speed (at least of elementary statements).

You should not worry at all: any good C++ compiler would optimize (at least when asked, e.g. for GCC with g++ -O or g++ -O2 etc...), probably giving the same machine code (or at least, code of similar performance).

BTW, how that is compiled and optimized heavily depends upon the target processor and the body of the if (e.g. because of branch prediction). Sometimes, the compiler would figure out that using conditional moves could be faster than a conditional jump.

With GCC, there are very few cases where you might help the compiler using __builtin_expect, but you should almost always not bother (and quite often bad or excessive use of that __builtin_expect would slow down the computation). You might consider also using appropriately and with great care __builtin_prefetch (see this) but again, you should usually not care.

Remember that premature optimization is evil (almost always).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    Performance is mentioned frequently in Stroustrup's books, if not the standard; it clearly drove a number of language decisions. –  Sep 25 '14 at 12:06
  • But the standard won't answer to questions like the one OP has asked. – Basile Starynkevitch Sep 25 '14 at 12:07
  • 1
    Sure; I don't want to even appear to argue as your answer is spot on; nevertheless it's a reasonable question; the standard document itself is neither as cheap nor readable as many other c++ books. –  Sep 25 '14 at 12:10
  • I linked to the latest draft which is believed to be very close (a few typo of difference) to the costly standard. – Basile Starynkevitch Sep 25 '14 at 12:12
3

Less interesting than the performance issue is simply that when you are dealing with classes (instead of Plain-Old-Data (POD) bool) they don't mean the same thing. One might invoke an overloading of == while the other does a boolean conversion.

Consider this:

#include <iostream>
using namespace std;

class Something {
public:
    operator bool() const {
        cout << "Running implicit cast to bool\n";
        return true;
    }
};

bool operator==(Something const & left, bool const & right) 
{
    cout << "Running overloading of ==\n";
    return false;
}

int main() {
    Something s1;
    if (s1 == true) {
        cout << "(s1 == true) branch ran.\n";
    }
    if (s1) {
        cout << "(s1) branch ran.\n";
    }
    return 0;
}

The output of that program will be:

Running overloading of ==
Running implicit cast to bool
(s1) branch ran.

It's a subtlety to be aware of. Although you cannot overload == for bool itself, so your statements are equivalent--and will almost certainly perform the same.

2

Although the real answer is depends on the compiler, I would say that in 99% of the casas is the same. In both cases.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292