Link to home
Start Free TrialLog in
Avatar of highqllc
highqllc

asked on

optimizing if() in VS 2005 c++

Hi,

I'd like to know whether VS 2005 (native c++ code, not .NET) automatically optimzes if() statements with multiple conditions to enter the block of code as soon as one of the conditions is true.

For example,

    if( isYummy() || reallyHungry() || haveGunPointedAtMe() ) {
                 eat();
     }

if the three functions all return a bool, then how will the compiler handle the entire if(). Will it execute isYummy() first?  What is the guaranteed order?  If it finds one of the functions to be true, will it call the others?  

What I _want_ is for the first function to be called and if not true, then the second to be called, and if not true, the third to be called.  My goal is to increase performance by not calling the last two functions if its not necessary.

Thanks

kevin
 
Avatar of highqllc
highqllc

ASKER

I should emphasize that it is absolutely critical that isYummy() called while I don't care of the other functions are called.
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But it is guaranteed that isYummy() is _always_ called in my example?  I.e., there's no strange conditions whereby the compiler would decide to switch up the order that the individual logic statements are evaluated?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> But it is guaranteed that isYummy() is _always_ called in my example?

Yes, but the others aren't guaranteed to be called.

The order in which they are called is the order in which you typed them.

So, conceptually, it's similar to :

     if(isYummy()) {
                 eat();
     }
     else if (reallyHungry()) {
                 eat();
     }
     else if (haveGunPointedAtMe()) {
                 eat();
     }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nice!  I didn't know that.
IMHO, if you REALLY want isYummy() to be called, instead of doing what you have, have it done this way:

bool myIsYummy = isYummy();
if( myIsYummy  || reallyHungry() || haveGunPointedAtMe() ) {
                 eat();
     }

Then you don't need to unroll the if() into 3 if-elses.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sleep_pilot : the first function will be called in any case, so there's no need to use your technique. However, it might be handy if you need the other two functions to be called in all cases :

    bool myReallyHungry = reallyHungry();
    bool myHaveGunPointedAtMe = haveGunPointedAtMe();
    if( isYummy()  || myReallyHungry || myHaveGunPointedAtMe ) {
                 eat();
     }
Infinity08: in the current version of compiler, that's true.  However, at high opt. the compiler may decided to move calls around.  (Not that I have an example of compiler that does that.)

I think, ultimately, it's best to not depends on side effect of functions.  For better software design, the side effect should be factored out to another function that will be called all the time.

YummySideEffect();
if (isYummy() || reallyHungry() || haveGunPointedAtMe()) {
  eat();
}

Done this way, the isYummy() code can be mark as const only, so this gives advanced compiler more freedom to inline the call (and less surprise for the developer 1yr down the road.)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Infinity08: Yes, you are right. I stand corrected.  It's in section 5.15 Logical OR operator [expr.log.or] of the C++ Standard.
May I ask why you gave a B grade ? Was something still not clear ? If so, then please tell us what, and we'll be happy to answer your further questions !
Hi,

I didn't think about the grade at all, honestly. Divying out points seems important to me, but it's not clear what the "grade" is used for.  Is it important?  I just hit "good" by default because most advice I get on here is just that, quite good.  ...Seems like A should be reserved for "outside the bell curve" answers that really blow one's socks off.  I can see your perspective too: if the question was answered adequately, an A should be given.    Well... nah... that's lame...    It's like in back in college, most professors would set the curve a bit high, at maybe a B+, elevating all grades a bit, making students feel good and proving their success in teaching the material.  Buth the hardliners would set the curve at a B- or C+, and literally force half of the students to get C's, D's, and F's even if they all knew the material quite well.   An "A" was exceptional.  I liked those professors. They gave an "A" real value.

In any case,  I will take the grade more seriously in the future if it really matters to the experts.

kevin
>> but it's not clear what the "grade" is used for.

This should explain it :

https://www.experts-exchange.com/help.jsp#hi73


>> In any case,  I will take the grade more seriously in the future if it really matters to the experts.

Well - it matters to know whether the answer you gave was correct and complete. As explained in the link above, a B grade usually means that the answer was incomplete - meaning that some work from your side was required to fill in the blanks. It also means that we as experts didn't give our 100% when answering your question, and that we could have done better.

That's all there is to it : I strive to give the best help possible, and a B grade usually indicates that there was something missing. If so, I'd like to learn from it, and try to do better by giving the missing information now and in the future.

I hope that makes sense to you ?
Cool. That link puts the grading system on a fairly objective level that I can try to be congruent with in the future. If it's possible to change grades after-the-fact, I can do that with this question.

Kevin
No need, Kevin :) If I know that you are happy with the answer, that's enough for me. But thanks for the offer.

Just fyi : should you in the future need to change or do something that you can't do yourself, you can post a 0-point question for that in Community Support :

https://www.experts-exchange.com/Community_Support/General/