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).