Moving computations to different places
Even with the current multi-core processors and several GHz of core frequencies, CPU power is still a scarce and precious resource. Every CPU cycle you waste by doing unnecessary calculations, using the wrong algorithms, or repeating operations is lost for the remaining parts of the program. Therefore, it is important to identify how to save CPU cycles while still doing the intended computations.
Recalculate only when necessary
There are essentially two opposite paths available to optimize code. You can try to optimize the code in a way that computes the results on every call with low overhead – or you can be lazy, cache the results, and recalculate new results only when some of the parameters have changed.
Both paths have their pros and cons. While continuously computed results will produce smooth and uniform calculation times in the functions, you do a lot of unnecessary operations if the input values never change. With the...