Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does short circuiting make execution of the program faster, and is analysing which statement to put first in the condition statement worth it? [closed]

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?

like image 419
Omid CompSCI Avatar asked Jul 22 '16 17:07

Omid CompSCI


2 Answers

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.

like image 79
rj99999 Avatar answered Sep 19 '22 20:09

rj99999


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!

like image 25
gsamaras Avatar answered Sep 19 '22 20:09

gsamaras