|
|
View previous topic :: View next topic |
Author |
Message |
yatoof
Joined: 01 Feb 2009 Posts: 4
|
c statements complexity? |
Posted: Mon Feb 16, 2009 8:10 am |
|
|
Hi, All is there any guide(link,book). which tells about complexity of instructions we use in controller's programming in c language e.g switch() is better or if statement.
to make program most efficient fast for execution. thanks.
.Yatoof _________________ Knowledge is Power |
|
|
Ttelmah Guest
|
|
Posted: Mon Feb 16, 2009 8:26 am |
|
|
No.
This will vary between C implementations, and even with specific circumstances. For example, in the case of the 'switch', CCS, will use exactly the same internal structure for this, and an if, in the circumstances that a 'default' statement is present. Remove the default, and in general, for consecutive values, a jump table will be used. As another example, in general, for most C implementations, a conditional expression, is more efficient than an 'if', but this isn't the case for CCS.
At the end of the day, it comes down to you looking at the assembler.
Best Wishes |
|
|
yatoof
Joined: 01 Feb 2009 Posts: 4
|
|
Posted: Mon Feb 16, 2009 8:45 am |
|
|
Thanks, Ttelmah
i understand that its mostly depends upon your compiler you are using. But in general terms like
if(SW) is better than if(SW==1). and other ways to have more efficient program using best equilent instructions. _________________ Knowledge is Power |
|
|
Ttelmah Guest
|
|
Posted: Mon Feb 16, 2009 11:15 am |
|
|
That might depend on the type of the variable. For an 'int1', the former would automatically evaluate as a simple bit test. The later might be done as an actual subtraction and test for zero on the result.
This is the problem, there are no simple 'rules'.
However for optimisation, there are some basic guides that should be applied:
1) Concentrate on whatever is done most often. Saving 1uSec, in a routine that is called a million times, is much more important than 1000uSec, in code that is called once.
2) Smaller arithmetic types are handled faster.
3) Multiplication is faster than division.
4) Try to take advantage of interrupt driven I/O to avoid _waiting_ for things. If (for instance), you printout 20 characters at 9600bps, and do this inside a routine, then this routine, has to take a minimum of about 18mSec to execute. Do the printing using a buffer, and INT_TBE, and you can be doing other things for probably 3/4 of this time...
Best Wishes |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Mon Feb 16, 2009 12:18 pm |
|
|
I'd say the necessary thing is to have an idea of what's most important to your program. If there are timing issues at all, where in the program do they apply? It obviously takes longer to waste 1 microsecond a million times than a millisecond once, but maybe that millisecond comes at a time when you can't afford it. So "it depends".
It really pays to have a reasonable idea of how your processor works. If there's hardware that can make your program run better, make sure you use it. And if there are awkward features which make the processor les efficient, plan to deal with them, or avoid them.
And don't be afraid to look at the LST file and check for places where there are huge stretches of hex lines generated by a single line of C. Sometimes you can easily rephrase the C code so as to generate much faster hex, without much need to understand the assembly instructions. |
|
|
|
|
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
|