3.5.4 How Can I Make My Code Faster?

To a large degree, smaller code is faster code, so efforts to reduce code size often decrease execution time. To accomplish this, see 3.5.2 How Can I Make My Code Smaller? and 3.5.6 How Can I Make My Interrupt Routine Faster?. However, there are ways some sequences can be sped up at the expense of increased code size.

Depending on your compiler edition (see 21 Optimizations), you may be able to use the -O3 option (see 5.7.7 Options for Controlling Optimization) to optimize for speed. This will use alternate output in some instances that is faster, but larger.

Generally, the biggest gains to be made in terms of speed of execution come from the algorithm used in a project. Identify which sections of your program need to be fast. Look for loops that might be linearly searching arrays and choose an alternate search method such as a hash table and function. Where results are being recalculated, consider if they can be cached.