-2

If I have to check one of two (or more) conditions to execute the rest of the scope inside a loop for example, which of these code is the best ?

while(condition){
  if(some_condition_1 || some_condition_2){
    continue;
  }

  // ... do stuff
}

In the case if we know that some_condition_1 will be more often true compared to some_condition_2, will separating the condition like below help optimize the code ?

while(condition){
  if(some_condition_1){
    continue;
  }
  if(some_condition_2){
    continue;
  }


  // do stuff
}

Since that if some_condition_1 is true, it won't have to check for some_condition_2 in the second case

aqww
  • 75
  • 6
  • 3
    For logical AND and OR operators, C++ employs [*short-circuit evaluation*](https://en.wikipedia.org/wiki/Short-circuit_evaluation). – Some programmer dude Nov 09 '17 at 20:38
  • 1
    write code to be readable and explicit and let the compiler do its job, it is usually much better at it than you are – 463035818_is_not_an_ai Nov 09 '17 at 20:39
  • `Since that if some_condition_1 is true, it won't have to check for some_condition_2 in the second case` That's actually true for both cases. – DimChtz Nov 09 '17 at 20:41
  • 2
    And a general tip: Don't worry about optimizations early on when writing your programs. Concentrate on making simple, readable, *maintainable* code. That works. Then if there's a requirement for specific performance, measure, benchmark and profile to find the bottlenecks, and take the worst one and optimize it (remembering to document and comment what you're doing and why). Then iterate as long as needed for the requirements to be fulfilled. And making sure the program passes all tests between each iteration. – Some programmer dude Nov 09 '17 at 20:41
  • Sorry for my previous misleading comment. It is not a compiler optimization; it is standard mandated. – patatahooligan Nov 09 '17 at 20:41
  • I am *quite* confident that your compiler will already optimize those trivial examples into the *exact same* thing (when you build with optimizations turned on). *Your* task is to write simple code like this in the manner that is most readable to other humans (including your future self). – Jesper Juhl Nov 09 '17 at 20:42
  • As other comments have implied, shortcircuiting of logical operators means there is no effective difference between your approaches. – Peter Nov 09 '17 at 21:12
  • Ugh - profile a release build and see if this is a "hotspot" in your code before you even dream of a micro optimization like this. – Michael Dorgan Nov 09 '17 at 22:02

1 Answers1

0

Write whatever is easiest for you to read, and compile with optimization.

That being said, in this case, short-circuit evaluation means that your first option is strictly better. Let's see why:

Assume the compiler is very dumb (no optimization). (I've removed your continues, since they also cause a branch, but the idea is the same.)

while(condition){
  if(some_condition_1 || some_condition_2){
    do_stuff();
  }

  do_different_stuff();
}

This will have to generate 1 branch instruction: at the if statement, depending on the value of the comparison, the instruction pointer MAY jump past the block containing do_stuff() and continue to the point immediately after it (in this case, do_different_stuff()). Depending on architecture, optimization, and the details of your code, an extra branch may be generated for the short-circuiting behavior of some_condition_1 || some_condition_2.

What about this version?

while(condition){
  if(some_condition_1){
    do_stuff();
    continue;
  }
  if(some_condition_2){
    do_stuff();
    continue;
  }
  do_different_stuff();
}

Well, first we will hit if(some_condition1), and the program will "branch" -- conditionally jumping to if(some_condition2). Here, the program will branch again, conditionally jumping to do_different_stuff(). This is two guaranteed branches! So the first option is better.

Jack Meagher
  • 306
  • 1
  • 6