Optimizing Loops

Loops are used regularly throughout source code. The loop constructs themselves are often considered as extraneous to the loop body that performs the main task at hand, but all parts of a loop can affect the speed at which it executes.

There are several things you can look at to improve loop execution speed. When considering the overall execution time of your program, any speed improvement you can make in the loop code is multiplied by the number of iterations the loop performs. Even small improvements in a loop can make a big difference overall if the loop iterates a large number of times.

The loop index variable can be accessed many times during each iteration of the loop. Consider the following code snippet that contains typical loop code, where N is defined as an arbitrary value.

In this above example, the index, i, is access three times per iteration, even though this is a simple loop example. It is important to ensure that the loop index variable has a type that can be efficiently accessed. See Choosing an Integer Data Type, which explains the fastest minimum-width integer types provided by the compiler, or alternatively for MPLAB® XC16 and XC32, use an int type and for MPLAB XC8 use a char type, if the number of iterations, N, can be held by these types. Choose an unsigned type if possible, as this might lead to even faster execution.

Let's rewrite the above loop using a different controlling expression.

Here we use a strong relational operator (!=) in the controlling expression, i not equal to N. This expression is checking for one single value: i is either equal to N or it is not. Although the weaker less-than operator (<) is commonly used by programmers, the strong operator can often produce smaller, faster code. This is because it is typically easier for code to check for a single value than for a range of values.

For some device instruction sets, counting down in the loop can lead to faster execution. If you do count down, check all the expressions in the loop that use the loop index. Ensure they are appropriate for the new range of values the loop index covers, especially the last value. In this example, the loop will terminate as soon as the loop index reaches 0. The array index expression in the body of the loop has been adjusted accordingly.

Look for other optimizations that might speed execution. For example, instances where an expression is being evaluated multiple times each iteration can be factored out, evaluated just the once, and stored for reuse. But before optimizing loop code—indeed any code—it is worth checking first that the compiler you are using is not already performing these optimizations for you. Above all, make sure your source code is easily readable and clearly indicates the operation you intend.

20th Annual
Microchip MASTERs Conference 2016

JW Marriott Desert Ridge Resort-Phoenix, AZ

© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.