3

Let's say I've this piece of code:

age = 21;
allowedFromParents = false;
if (age >= 18 || allowedFromParents == true)
{
    //go drink
}

When I'm debugging this part of code, when it checks the first part and that's true, then it goes already inside the statement without bothering to check the other statement.

Now, this is just an easy condition, but what if the condition statements were much larger to calculate, are inside a frequent loop and one will return a true more likely than the other.
Will then the order of the conditions matter for performance?

Steven
  • 1,996
  • 3
  • 22
  • 33
  • 6
    [Short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation) – Damien_The_Unbeliever Jan 04 '18 at 08:31
  • 1
    Are you asking this just for knowledge sake or do you really have a performance problem with the condition order in a OR evaluation? It seems obvious that if the first condition returns true more often than the latter you will have better performance but it depends more on what you do in those conditions evaluation – Steve Jan 04 '18 at 08:33
  • Related: https://stackoverflow.com/questions/35301/what-is-the-difference-between-the-and-or-operators – Drag and Drop Jan 04 '18 at 08:35
  • @Steve mostly for knowledge sake. I was almost certain that it would impact performance, but I was curious if there was a better explanation to what I was thinking about it. – Steven Jan 04 '18 at 09:27
  • And recently I've discovered that you can change statement priorities for many other uses besides performance, it can also be used to shorten up single if-statement. For example it's also possible in a single statement to check if a class isn't null and right afterwards check the values inside the class. It's nice to understand the logic of code more as you're learning. – Steven May 24 '18 at 06:39

4 Answers4

4

Yes, it matters. if the first condition is true then it will stop evaluating the rest. In another word, the condition on the right might not be evaluated.

The logical OR operator (||) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool prior to evaluation, and the result is of type bool. Logical OR has left-to-right associativity.

https://msdn.microsoft.com/fi-fi/library/f355wky8(v=vs.120).aspx

Ray Krungkaew
  • 6,652
  • 1
  • 17
  • 28
4

What you are seeing is called short-circuit evaluation.

As you guessed, the order of the condition statements could affect performance: if one of them is much more heavy to calculate and it is done in a tight loop, you could consider putting the other one first.

However,

Premature optimization is the root of all evil -- DonaldKnuth

meaning that unless you have measured a performance bottleneck in this code, you should write it with readability in mind, not performance.

Tao Gómez Gil
  • 2,228
  • 20
  • 36
1

put the condition which would take more time to execute on the right so if the condition on the left evaluates to true then the second condition wouldnt be checked.

and yes if you are in a tight loop which would iterate lets say a thousand times in worst case then it would definitely improve performance

Fakhar Ahmad Rasul
  • 1,595
  • 1
  • 19
  • 37
0

Yes, the priority of these statements can matter, but not just about performance.
In some cases it can be used to shorten up multiple if-statements.

For example:

if (patient == null || patient.age == 0)
{
    //patient is invalid
}

I formerly thought that this only could work well with and-statements, but in this case it won't reach the second statement unless the patient is not null. making it more efficient in readability, performance and, in my case, more secure for crashes.

Steven
  • 1,996
  • 3
  • 22
  • 33