|
|
View previous topic :: View next topic |
Author |
Message |
jecottrell
Joined: 16 Jan 2005 Posts: 559 Location: Tucson, AZ
|
Nested IF or && |
Posted: Wed May 04, 2005 9:36 am |
|
|
Which is more efficient:
or:
Thanks,
John |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed May 04, 2005 12:50 pm |
|
|
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
|
|
Posted: Wed May 04, 2005 2:55 pm |
|
|
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
|
|
Posted: Wed May 04, 2005 6:54 pm |
|
|
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
|
|
Posted: Thu May 05, 2005 2:22 am |
|
|
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 |
|
|
|
|
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
|