A short circuit speeds up the execution of the program and analyzes which operator to put first in the condition expression?

For example (let's say we are talking about C ++, if this is different), the B && operator, if I know that one operator will lead to 0 more often / has a higher chance, then the other operator should put this on the left side, and the other operator right?

The same for || if I know that one operator will lead to 1 more often / has a higher chance, then the other operator should put this on the left side, and the other operator on the right?

Now all this will cause a lot of time analyzing the program, but if it speeds up the execution time of the program, is it worth it to do, and this is what the built-in / system programmers look at in real time to speed up their application if necessary?

+7
c ++ performance logical-operators short-circuiting
source share
5 answers

First, make sure that you are not a victim of premature optimization.

With that said, make sure you do your best to speed up the bottleneck in your program.


Doing what you said about the short circuit may be a good idea in some cases, but it depends a lot on all of your statements.

For example, if you have something like:

if(slowFunction() && complexConditionRootsAndExponents && ConditionUsuallyZero) 

then you probably want the last term to be the first, right?

However, be careful, things are not always trivial to rearrange in a logical sequence. Check for example my answer in Why did this program print fork 4 times? where you can see that a short circuit can affect the flow of the program.


TL; DR

In general, however, it is rarely possible to achieve significant acceleration by rearranging terms in conditions. Focus on the bottleneck of your program and deal with it as much as you can!

+4
source share

It depends. If the statement is as simple as:

 if(y == 4 || x == 2) 

and suppose that the frequency x == 2 is much higher, so we could short-circuit the performance by writing:

 if(x == 2 || y == 4) 

But you see that we will not get much benefit from this, since the statements are very simple and the optimization of the code at this level may not be so worthy.

Now consider an example of the type:

 if(y == an_expensive_function() || x == 2) 

Here it is assumed that an_expensive_function () is a very expensive operation, let's say that the complexity is exponential, it definitely makes sense to put the operator as:

 if(x == 2 || y == an_expensive_function()) 

to perform a short circuit.

Embedded and application developers or any developer in the first instance may not consider optimization with such a fine granularity, if this does not give them big advantages. Perhaps they won’t even think about it if everything is good for them. So, the developer needs to check how much time it will take to analyze and optimize the code at this level and how many benefits we get from this.

+5
source share

Answer to the question: Yes, this affects performance.

Regardless of whether it is worth winning performance, whether it is worth finding places that can be improved and change the program, you can only answer.

In most cases, the change in productivity will be small, but if some of the operations involved are expensive, it can be significant.

Remember that there may be the right consequences. For example, if in if (foo() || bar()) it is important that bar never called, if foo returns true, then it would be an error to reorder the calls.

Start by ensuring the correctness of your program. Then if it is too slow; profile it and optimize where it will have the greatest impact. This may be an evaluation order in the context of a short circuit, but in most cases it will be something else.

+4
source share

You should also consider how expensive each side score is.

 if (veryCostlyOftenFalse() && veryCheapRareFalse()) // may be faster other way around 

If more than 50% of your source is not expression evaluations and branching, I would say that this is optimization in the last place when you are happy with everything else.


Embedded programmers of real-time applications are oriented in approximately the following order:

  • Of course, find a reasonable compromise in speed and space.
  • data structures in memory (caches hit as often as possible when they are executed by these algorithms).
  • profiling a real application with real data to see if there are any unexpected bottlenecks and how to fix them.
  • If you have desperately gone missing for hours or two, and there are some complicated if , then yes, that might help ...
+3
source share

Of course. if your conditional has the form:

 if ( x() && y() ) ... 

and y is expensive to compute, and x is cheap and often does not work, this will improve local code performance.

So you want to know:

  • It is conditional in part, depending on the performance of the program (if not, there is no point in optimizing it, writing for clarity)
  • relative costs of computing the components of the expression of the short circuit
  • which cheap computing fails (for &) or succeeds (for ||) often.

In this case, it is usually worth reordering the elements of the short circuit expression.

+3
source share

All Articles