CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Nested IF or &&

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

Nested IF or &&
PostPosted: Wed May 04, 2005 9:36 am     Reply with quote

Which is more efficient:

Code:

if(x) {
  if(y) {


or:

Code:

if(x && y) {


Thanks,

John
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed May 04, 2005 12:50 pm     Reply with quote

The best way to find out is to write yourself a little test program, compile it, and look at the .lst file.
The answer may depend on:
1) The compiler version
2) The optimization level
3) The types of the variables
4) Whether you are looking for small size or high speed
_________________
The search for better is endless. Instead simply find very good and get the job done.
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Wed May 04, 2005 2:55 pm     Reply with quote

Thanks.

I'll try that. I'm looking mostly for speed (I have yet to outgrow a part yet.)

I assume that less instructions will be faster, but it will also be based on what type of instructions get used (because different instructions take different numbers of steps?)

John
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Wed May 04, 2005 6:54 pm     Reply with quote

Instructions all execute in a single system clock (1/4 the Fosc rate) except for program branches.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Ttelmah
Guest







PostPosted: Thu May 05, 2005 2:22 am     Reply with quote

I'd 'expect' that the answer will depend on the program flow. In the first case, if the condition fails, the program will branch, and not execute the second test, and be faster. However if he condition 'passes', then there will have been the extra instruction times associated with this branch, before it executes the second test (remember a 'skip not zero' type branch, uses 2 instruction times if it either skips, or does not).
So potentially, if you can have the most common program flow being the condition passing the least tests, this will give the fastest normal execution, but possibly at the 'cost' of actually taking longer for the states where a lot of tests are involved. The compiler does not perform the optimisation often associated with 'sequential' logic tests, of performing them seperately (on some compilers, if you perform a pait of 'anded' tests as shown, it performs the first test, and if it solves to 'false', knows there is no point in performing the second - this gives exactly the same structure as the sequential 'if' statements).
The answer will also depend on the nature of 'x', and 'y'. If for instance these statements involve an array access, there will be a massive overhead in this, and the potential advantage of skipping the second test rises.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group