-1

Let's say that we have two C++ code snippets:

int x=3;  
if(x)  
{
//do stuff  
}  
int x=3;  
if(x!=0)  
{ 
//do stuff  
}  

Which one is more efficient in terms of computing speed? The first one is more convenient to write as it's shorter, but I'm not sure it's also faster.

John
  • 181
  • 9
  • 2
    Exactly identical as performance goes. I'd argue for `x != 0` though, since it's clearer that we're working with a numerical value. If you had a boolean or a pointer, then doing `if (x)` is fine. But this is all preference. – DeiDei Jan 17 '20 at 17:50
  • Equal as said, don't try to outsmart compiler if you don't know how they works and you really need. Try your best to keep your code clear. – Damiano Jan 17 '20 at 17:53
  • 1
    @John The more efficient if(x) because it is less error prone. For example sometimes the condition if(x!=0) is written by accident like if(x=!0) . :) – Vlad from Moscow Jan 17 '20 at 17:55
  • dont forget: you dont write for the cpu, you write code for the compiler targeting an abstract machine, its the compiler that takes care of translating it into something your cpu understands – 463035818_is_not_an_ai Jan 17 '20 at 17:55
  • 1
    @formerlyknownas_463035818 Generally speaking you write code for people.:) – Vlad from Moscow Jan 17 '20 at 17:58
  • @VladfromMoscow Do you work for a company that employs people to execute programs on paper? :) – eerorika Jan 17 '20 at 18:01
  • @eerorika No, but the people read my code on paper.:) – Vlad from Moscow Jan 17 '20 at 18:02
  • 2
    In c++, the code you write is meant to *describe a behavior*. `if(x)` and `if(x != 0)` describes the exact same behavior where `x` is an `int`. If the assembly equivalent of one would be faster than the other, you should assume that the compiler will be smart enough to perform the transformation. – François Andrieux Jan 17 '20 at 18:11
  • 3
    Something that isn't always obvious to new c++ programmers is that a c++ compiler doesn't just do a direct 1-to-1 translation of c++ source code into corresponding CPU opcodes. Rather, it converts the c++ source into an internal low-level representation, and then it runs a very elaborate series of optimization transformations on that, before finally translating the low-level instructions into the appropriate CPU opcodes. After all the optimization steps have completed, the program is guaranteed to behave the same as the original c++ code, but may be implemented quite differently. – Jeremy Friesner Jan 17 '20 at 18:18
  • @Jeremy Friesner Doesn't that happen only when you use the -O flag though? – John Mar 06 '20 at 23:13
  • @John -O enables the more elaborate optimizations, but many trivial ones (e.g. `if (x)` vs `if (x!=0)`, as suggested in this question) will happen regardless, as a side effect of the way the compiler and the language are designed. (And note that there's not much point in considering the behavior of non-optimized code when talking about computing speed, because if you're at all concerned about computing speed, the first thing you will do is enable optimizations) – Jeremy Friesner Mar 06 '20 at 23:28

2 Answers2

2

Which one is more efficient in terms of computing speed?

Neither.

Some coding conventions suggest using the latter, but the first can be considered just as readable and conventional. Choosing between them is a matter of taste.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

Which one is more efficient in terms of computing speed?

if (x) and if (x != 0) runs at the same speed.
They will both do a comparison.

The first one is more convenient to write as it's shorter, but I'm not sure it's also faster.

Depends on the intention. If you're checking a pointer, then if (p) is more clear (IMO)

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • sorry for being nitpicky, but imho already "runs at the same speed" is misleading. Most of the time you dont need to care if a piece of C++ code corresponds to instructions that are actually carried out by the hardware and that is a good thing. If the compiler can infer that `x` is never zero it is possible that in the final program nothing "runs" for this condition – 463035818_is_not_an_ai Jan 17 '20 at 18:16
  • 1
    @formerlyknownas_463035818 "nothing" runs at same speed as "nothing". – eerorika Jan 17 '20 at 18:33