Sometimes you'll encounter loops, or recursive functions, that take a long time to execute and are
bottlenecks in your product. Before you try to make the loop a little faster, spend a few minutes
considering if there is a way to remove it entirely. Would a different algorithm do? Could you
compute that while computing something else? If you can't find away around it, then you can optimize
the loop. This is simple; move stuff out. In the end, this will require not only ingenuity but
also an understanding of the expense of each kind of statement and expression. Here are some suggestions:
â€¢ Remove floating point operations.
â€¢ Don't allocate new memory blocks unnecessarily.
â€¢ Fold constants together.
â€¢ Move I/O into a buffer.
â€¢ Try not to divide.
â€¢ Try not to do expensive typecasts.
â€¢ Move a pointer rather than recomputing indices.
The cost of each of these operations depends on your specific system. On some systems compilers
and hardware do these things for you. Clear, efficient code is better than code that requires an understanding
of a particular platform.