问题
For instance (Lets say we are talking about C++ if that makes a differnce), In an && operator if I know that one statement will result to 0 more often/has a higher chance then the other statement should I put that on the left side, and the other statement on the right?
Same goes for || operator if I know that one statement will result to 1 more often/has a higher chance then the other statement should I put that on the left side, and the other statement on the right?
Now doing all this would cause a lot of time analysing the program, but if this does speed up execution time for the program is it worth doing it, and is this something that embedded/real-time system programmers look into to speeding up their application if necessary?
回答1:
First, make sure you are not a victim of premature optimization.
With that said, make sure that you did everything you could to speedup the bottleneck of your program.
Doing what you said about the short circuiting may be a good idea in certain cases, but that's heavily depends all your statements.
For example, if you have something like:
if(slowFunction() && complexConditionRootsAndExponents && ConditionUsuallyZero)
then you would probably want that last term to be first, wouldn't you?
However, be careful, things are not always trivial to permute in a logical sequence. Check for example my answer in Why this program printed fork 4 times?, where one can see that short circuit can affect the flow of the execution of the program.
TL;DR
In general though, it is rare to get significant speedup by permuting the terms in the conditions. Focus on the bottleneck of your program and tackle that as hard as you can!
回答2:
It depends. If the statement is as simple as :
if(y == 4 || x == 2)
and assume that frequency of x == 2 is much higher, so that we could have short-circuited the execution by writing like:
if(x == 2 || y == 4)
But you see we wont be getting much benefit out of this, as the statements is very simple and optimizing the code at this level may not be so worthy.
Now consider an example like :
if(y == an_expensive_function() || x == 2)
Here assume an_expensive_function() is very costly operation, say it's complexity is like exponential, the definitely it makes sense to put the statement like :
if(x == 2 || y == an_expensive_function())
to perform short-circuiting.
Embedded and application developers or any developer at first instance might not consider optimizing at such a fine granulaity if this is not giving them much benefits. They may not even consider it if things are working fine for them. So as a developer we need to check, how much time will it take to analyze and optimize the code at such a level and how much benefits do we get from this.
回答3:
The answer to the question is: Yes, it does impact performance.
Whether or not the performance gain is worth the cost of finding the locations that can be improved and changing the program is something only you can answer.
In most cases the performance change is going to be small, but if some of the operations involved are costly, it can be significant.
Be aware that there can also be correctness implications. For example if in if (foo() || bar())
it is important that bar
is never called if foo
returns true, then it would be a bug to re-order the calls.
Start by ensuring your program is correct. Then, if it is too slow; profile it and optimize where it will have the biggest impact. That may be the order of evaluation in a short-circuit context, but in most cases it will be something else.
回答4:
You also have to consider, how costly the evaluation of each side is.
if (veryCostlyOftenFalse() && veryCheapRareFalse()) // may be faster other way around
Unless over 50% of your source are expression evaluations and branching, I would say this is last-resort optimization, when you are happy with everything else.
The embedded/real-time application programmers focus roughly in this order:
- algorithm of course, finding reasonable trade-off in speed vs space.
- data structures in memory (hitting the caches as often as possible, when exercised by those algorithms).
- profiling of real application with real data, to see if there's some unexpected bottleneck and fixing those.
- if you are desperately missing somewhere a clock or two, and there's some complex
if
around, then yes, it may help...
回答5:
Sure. if your conditional is of the form:
if ( x() && y() ) ...
and y is expensive to compute, and x is cheap and fails often, this will improve the local performance of the code.
So you want to know:
- is the conditional in a performance-sensitive part of the program (if not, no point in optimizing it, write for clarity)
- relative costs of the component computations of the short circuit expression
- which cheap computations fail (for &&) or succeed (for ||) frequently.
In this case it is usually worth rearranging the short circuit expression elements.
来源:https://stackoverflow.com/questions/38532160/does-short-circuiting-make-execution-of-the-program-faster-and-is-analysing-whi