Common Optimization Tips

This tip submitted by Travis Fischer on 2005-05-14 11:45:38. It has been viewed 60216 times.
Rating of 6.9 with 389 votes



The first and most important thing to remember when optimizing a program is to know your compiler's options. Most of the time, more can be done just by adjusting compiler options than by anything else; it's just that many programmers aren't familiar with the different switches available. Since most compilers have different switches, I'd suggest looking through your compiler's help docs to see which switches would be best for you. If unsure about a switch, try them to see if they are beneficial -- if not, then don't use them (make sure to note any possible side effects in the docs for that switch).

Other hand optimizations: (keep in mind that premature optimization can be worse than no optimization at all - never assume that an optimization is beneficial because every instance of an optimization may be different)

* For loops - If the order of the iterations in a for loop does not matter and you are starting from zero, then replace:

for(i = 0; i < max; i++) {
list[i] = 0;
}

with:

for(i = max; i--;) {
list[i] = 0;
}

It will be slightly faster and should be 2 bytes smaller -- not much in and of itself, but spread over a large program or in many nested for loops and it does make a difference.

* Declare functions called only once or twice as "static inline". Static inline functions can usually be inlined better than plain inline ones. Also, the compiler will usually only inline if it will produce better code.

* If you have a lot of global variables, try putting each one in a typedef struct and then using a global register variable to access them. Then, access them through the global register variable as a pointer like:

typedef struct {
data vars...
} GlobalVars;

register GlobalVars *g asm(\\"a4\\");

and allocate/initialize g to be used throughout your program, accessing the data as g->data.

* Think about unrolling speed critical or smaller loops.

* If you have several memory allocations to do, try allocating with one bulk malloc instead of several smaller ones; then split up the appropriate pointers afterwards. Having many memory allocations and the associated error checks that accompany them may not be as efficient as one bulk allocation. Keep in mind, though, that by allocating memory in one large chunk instead of several smaller chunks, the allocation is more likely to fail, especially if you are dealing with large amounts of memory or are running on an older system.

* It may be worth it to learn some assembly, even if it is just for the purpose of inline assembly for speed critical parts where your program gets bottlenecked. Though compilers are closing the gap between hand-optimized assembly and C/C++, they are still not at par with them. Using inline assembly is sound advice, but you also have to remember that novice-written assembly will more than likely turn out to be less efficient than comparable compiler-outputted code.

* And last, but definitely not least, optimizations are pointless if you are using an inefficient algorithm!
{stupid metaphor} Walking 50 miles, even if you optimize your route, will still be slower than driving 50 miles on unoptimized, curvy roads {/stupid metaphor}
Walking (inefficient algorithm), even if optimized, will not be as fast as driving (more efficient algorithm).

-- Fisch2 :)





More tips

Help your fellow programmers! Add a tip!