Monday, 12 February 2007

Top 5 Optimisation Techniques

This post is aimed at Engineers who, along the way, forgot how to effectively optimise. I am not claiming to be the oracle of optimisation but after working with several different platforms, including CE [argh], I have picked up a number of quick and easy tips to help speed up your program. Most of the optimisation techniques I talk about aren't language specific so you can apply them to any project your are working on.

So anyway here is my top 5 methods to optimise code in any language:

1. Remove unnecessary code.
If the code isn't needed, then lose it. Use only the code you need to use to get the job done. If you have re-written a function or class then make sure you remove all the old code fully as it will just slow your program down.

2. Move code out of loops
Another big drain on CPU is having code within tight loops. By tight I mean loops with many iterations. This can include everything from variable declarations to pre-processing values outside the loop. Every little can help especially when speed is essential. Remember to also look at function calls within tight loops. The function call itself can have a slight overhead, but the main importance is to make sure the function itself isn't bloated. For more information regarding tight loop optimisation check out our post on Duff's Device

3. Pass objects by reference rather than value
Passing around objects can be very costly as they are copied every time another function or class uses them. To get around this pass objects by reference or pointer where possible and the memory copied will be reduced to the size of your pointer [4-8bytes].

4. Pre Allocate memory
Memory allocation, and manipulation, is one of the slowest operations you can do as a programmer. To avoid unnecessary hold ups try allocate memory before you need it. It is also advisable to block allocate memory rather than allocate little bits at a time as it is more efficient in both allocation and subsequent referencing.

An example of this would be to use an array rather than a list [standard library]. A list allocates memory as and when it needs it so the memory allocated for a particular object in the list is not guaranteed to be adjacent to the previous object. So pre allocate your array [maybe at start up] so you have no slowdowns later on.

5. Minimise and optimise disk access
Another hog on you application is disk access. Hard disks, or flash drives are a LOT slower than manipulating data in memory so care should be taken when writing out files. You will find that writing a bytes at a time is slower than just flushing through a lot of data. This depends on the disk medium you are writing to but generally writing out in bigger chunks [e.h 64k] is much more efficient. It is best to experiment on your particular platform when writing out data to avoid other bottle necks [write buffers for one can slow you down].

So there you go, 5 little tips that you already knew but probably forgot. Bear these in mind next time you are wondering why your next masterpiece is running a little sluggish.

3 comments:

Krishna said...

good stuff.
JavaBeat

Richard Jonas said...

I certainly agree with points 1 and 3. I think 2 and 4 are often best left to the compiler to optimise - modern compilers are good at this and it's better to have readable code in your application.

I would change point 5 to "Minimise the amount of times data has to be moved between parts of your system". Modern operating systems deal with caching disk accesses as well as you could in most cases, but if you've structured your program so data regularly moves between two parts of a system, it is a lot slower than moving it within the system.

nerd1951 said...

For item 4 sometimes it's hard to know at startup how much memory you're going to use, or you need to dynamically allocate and deallocate the same objects over the lifetime of your program. In these cases sometimes you can gain a lot by writing your own allocator. The boost library (boost.org) and the book "Modern C++ Design" both have examples of specialized memory managers.