Book Search

Download this chapter in PDF format


Table of contents

How to order your own hardcover copy

Wouldn't you rather have a bound book instead of 640 loose pages?
Your laser printer will thank you!
Order from

Chapter 4: DSP Software

Execution Speed: Programming Tips

While computer hardware and programming languages are important for maximizing execution speed, they are not something you change on a day-to day basis. In comparison, how you program can be changed at any time, and will drastically affect how long the program will require to execute. Here are three suggestions.

First, use integers instead of floating point variables whenever possible. Conventional microprocessors, such as used in personal computers, process integers 10 to 20 times faster than floating point numbers. On systems without a math coprocessor, the difference can be 200 to 1. An exception to this is integer division, which is often accomplished by converting the values into floating point. This makes the operation ghastly slow compared to other integer calculations. See Table 4-6 for details.

Second, avoid using functions such as: , etc. These transcendental functions are calculated as a series of additions, subtractions and multiplications. For example, the Maclaurin power series provides:

While these relations are infinite in length, the terms rapidly become small enough to be ignored. For example:

sin(1) = 1 - 0.166666 + 0.008333 - 0.000198 + 0.00002 - …

These functions require about ten times longer to calculate than a single addition or multiplication (see Table 4-6). Several tricks can be used to bypass these calculations, such as: x3 = x·x·x; sin(x)≈x, when x is very small; sin(-x) = -sin(x), where you already know one of the values and need to find the other, etc. Most languages only provide a few transcendental functions, and expect you to derive the others by means of the relations in Table 4-7. Not surprisingly, these derived calculations are even slower.

Another option is to precalculate these slow functions, and store the values in a look-up table (LUT). For example, imagine an 8 bit data acquisition system used to continually monitor the voltage across a resistor. If the parameter of interest is the power being dissipated in the resistor, the measured voltage can be used to calculate: P=V2/R. As a faster alternative, the power corresponding to each of the possible 256 voltage measurements can be calculated beforehand, and stored in a LUT. When the system is running, the measured voltage, a digital number between 0 and 255, becomes an index in the LUT to find the corresponding power. Look-up tables can be hundreds of times faster than direct calculation.

Third, learn what is fast and what is slow on your particular system. This comes with experience and testing, and there will always be surprises. Pay particular attention to graphics commands and I/O. There are usually several ways to handle these requirements, and the speeds can be tremendously different. For example, the BASIC command: BLOAD, transfers a data file directly into a section of memory. Reading the same file into memory byte-by-byte (in a loop) can be 100 times as slow. As another example, the BASIC command: LINE, can be used to draw a colored box on the video screen. Drawing the same box pixel-by-pixel can also take 100 times as long. Even putting a print statement within a loop (to keep track of what it is doing) can slow the operation by thousands!