root@fandigunawan

December 2, 2007

Code Optimization for Your Program

Filed under: Microcontroller, Misc, Software Development — Tags: — fandigunawan @ 6:57 am

A few weeks a go, I tried  to make a program in WinAVR for AVR ATMega8 for driving Nokia 3310 LCD (pcd8554). The driver which I wrote is actually big (what the heck) 28% of 8kb. This is only for the driver without other functions that I will implement soon. Hmm I need something to get rid that fatty code. The thing that I always remember is knowing your compiler and source code written. The PDF I read was taken from http://www.fefe.de/know-your-compiler.pdf. Even this PDF discussing about source code optimization for PC, IMHO both PC or microcontroller is the same thing except its circumstances.

IMHO, compiler will do their best part of creating a program (binary), however the other part is programmer himself. The quality of a code will directly impact to program performance and size.

There are two things which could be optimized:

  • Speed
  • Size

For example, in my case I need a medium performance but with very best code size. Actually, I could optimize the code by putting parameter into compiler. However, it is not good enough for the program. So, code optimization is the thing that I really should do.

There are some important things that we should consider while typing a source code:

  • Variable types

Variable types will definitely optimize your program since you will optimized it until the last bit of program memory space or RAM. For example in 8 bit microprocessor, creating a useless 16 bit integer will only waste our RAM and program spaces. The “useless” means that actually we could choose other variable type e.g byte or char. The other thing is that we could put unsigned symbol for case where we dont need negative number.

  • Limit string or pointers

String indeed will place crucial place for your program. Limiting string size or unpredictable pointers will save our job. String or pointer must be clearly initialized and predicted its worst condition (maximum) condition.

  • Optimized repetition

This thing sometimes trick our eyes because the repetition is actually “style of our culture”. A bad habit. Sometimes, making an easy-to-read repetition will cost program performance. There are also some place where a long repetition will also give us misery during program development because of its performance does not meet our criteria.

  • Lookup is faster but waste of space

Yeah… lookup table is very important for people who have less consideration in program speed. If you still have a big unused space you may use lookup table to make program computation faster.

Example:

Binary Coded Decimal routine would be better implemented using lookup table.

  • Tricks

Yes… tricks will work. Sometime we need to consider this example:

x = y*4;

But what if we change it this way?

x = y <<2;

In most of processor or microcontroller, multiplying take a longer cycle than bit shifting.

  • You will always need to see instruction set architecture for special code provided by the vendor.

MMX, SSE, SSE2 etc. are the examples of special features which actually we should use for computation. A good program should utilize the processor best features rather than wasting time creating new routine for computation.

Example:

We might not need to create new routine for 64 bit integer computation for Intel processor with MMX feature. It will waste our energy and degrade program performance. It is really suggested to use MMX instruction for 64bit computation. It is faster since it is “natively” available in semiconductor.

Important links:

 

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: