2

Just a basic question on IF statements in programming languages, specifically C++. Consider the following basic code example:

int i = 2;

if(i == 2 || i == 4) {
    //do something
}

Because the first condition would equate to true, are CPU cycles wasted on the second condition? Sorry for the Programming 101 question, just would like to know.

  • 3
    stackoverflow.com/questions/1799072/… would be the question on SO that is really similar to this. – JB King Jun 24 '16 at 3:20
  • 1
    Thanks, this can be removed as a duplicate then. This was not coming up in my searches. – Bob Jun 24 '16 at 3:21
  • 1
    I might be wrong but I think that whole statement would be removed by the compiler on account of its redundancy. – Alternatex Jun 24 '16 at 8:18
  • 1
    Similar statements about "waste CPU cycles on" can be made for "branch predictor guessed wrong and stalled the pipeline" for void foo(int i) { same conditional }. This is exactly the sort of thing that you should completely ignore, as compiler + cpu machinery exist to do that micro-optimisation for you. – Caleth Jun 24 '16 at 10:35
4

Possible compiler optimization aside, C and C++ language specs explicitly says that expression such as if(i == 2 || i == 4) will be evaluated left to right and second part after || will not be evaluated if first part is true. This is particularly important when right part after || is an expression that has side effects. For example, if it is a function call that function will not be called. This rule is often exploited by some (rather tricky) C/C++ code pieces.

  • 2
    "Possible compiler optimization aside" I think the people mentioning these optimizations are missing the spirit of the question, which makes your answer better. Anyway, this is called short-circuit evalutation – nanny Jun 24 '16 at 14:16
  • Thank you @nanny for this link. I didn't know (or at least forgot) this term – mvidelgauz Jun 24 '16 at 14:28
0

For one, that's not the kind of "wasted CPU cycles" that you need to worry about. Even if the second condition was checked, that's a cycle or two. Beginners worry about nanoseconds. Experienced programmers worry about microseconds (I used to say they worry about milliseconds, but times are changing). It's called micro-optimisations and is the kind of thing that produces unreadable code without giving any speed advantages, and later on prevents real optimisation because the code is too hard to maintain.

By the definition of the language, in C, C++, Objective-C, Java, C# and probably many other languages, it is well defined that a logical or evaluates the left side first. If the left side is non-zero, then the right side is not evaluated at all. If the left side is zero, only then is the right side touched.

(Compilers are free to actually do what they like as long as it doesn't change the official behaviour which they will do if it is faster).

And in your particular case, after an assignment i = 2 the compiler won't produce code to do any comparison, because it knows the outcome already. i == 2 is true, therefore i == 4 isn't evaluated at all.

  • The code example in the question seems misleading. Would any part of that whole evaluation survive the compilation process? – Alternatex Jun 24 '16 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.