|
||||
Optimization for PIC MicrocontrollersThis page will help you to optimize your PIC C code. The techniques for optimizing C code on a PIC can be very different to optimizing for a PC - sometimes the complete opposite of what you might think! You must first decide exactly what you are optimizing for: size or speed. These two goals are sometimes (but not always) conflicting. The second thing you have to decide is where to optimize. Your program will probably spend 90% of its time running 10% of your code, so obviously it is important that the 10% it uses most should be the best optimized. This means your inner loops, interrupt service routines, and so on. You probably won't be able to do any profiling to actually measure where the time is being spent, but you probably know your code well enough to guess where the bottleneck is. I will mostly be describing PIC-specific optimizations, with perhaps a mention of one or two standard optimization techniques. For more general optimization techniques, consult your favourite search engine! This tutorial was written by MrZebra, October 2007. Use Better AlgorithmsCode optimization and bit twiddling will only get you so far. The very best way to optimize your code is to use a better algorihm. This may mean replacing your linear search with a binary search, replacing your shift register with a circular array, and so on. What you actually do will be entirely depenent on your application - but this is by far the best method of optimization! Forget your PC OptimizationThe PIC has no cache, no branch prediction, no page file, and only one working register. Data locality means nothing on a PIC! Use Unsigned CharThis is very important : don't use
Avoid PointersThat's right! Pointers were your best friend when optimizing for the PC, but on the PIC they are not. Pointers on the PIC are 16 bits long, but the PIC is only an 8 bit processor. This means means that things like pointer addition ( Good: Bad: Avoid Loading ConstantsLoading a constant such as
Avoid Overusing ShiftsUsing a bit shift is a great way to divide/multiply by a power of two, but the PIC can only shift one bit at a time... so to shift 3 bits requires 3 instructions. Keep this in mind!
Check the Assembly OutputOnce you've compiled your program, go to "View Disassembly" on the menu (if using MPLAB... if not, your compiler probably writes it to a file somewhere that you can find). Find your inner loop, and see if the compiler is doing anything stupid. Keep a copy of your assembly code, tweak your C code, recompile, and compare the new assembly output side by side with the old. This is a good way to tell if refactoring your C code is making things better or worse. If you really can't get the compiler to write it how you want, consider using inline assembly. Single Bits are CheapThe PIC has instructions to set, clear, test and toggle a single bit - use them!
Use Lookup TablesA standard optimization technique - store things in a table. Look carefully at your memory usage and remember: free memory is wasted memory. It might go against the grain to create a lookup table for something that could be easily handled by a Unrolling LoopsA nice and simple technique to start with: unrolling loops. This simply means that instead of going around a Be aware that this method trades size for speed - your code will become larger, but faster. Use this technique very sparingly: it's most useful for things like copying arrays.
|
||||