Putting On Your Thinking Cap

The best optimizations usually happen when people really think about the problem (See the book Programming Pearls, by Jon Bentley (Addison-Wesley, 1986), for more on this approach). I remember once at TMS we had to obtain the sine of some number of degrees many times in a loop. We used Visual Basic’s Sin routine to provide this functionality and ultimately built the application and profiled the code. We found that about 90 percent of all our recalculating execution time was spent inside the Sin routine. We decided therefore to replace the call to Visual Basic’s routine with a call to a DLL function that wrappered the C library routine of the same name. We implemented the DLL, rebuilt, and retested. The results were almost identical. We still spent most of the time inside the Sin routine (although now we had another external dependency to worry about—the DLL!). Next we got out the C library source code for Sin and had a look at how we might optimize it. The routine, which was coded in assembly language, required detailed study—this was going to take time! At this point, someone said, “Why don’t we just look up the required value in a previously built table?” Brilliant? Yes! Obvious? Of course!