Advantages
The overhead in "tight" loops often consists of instructions to increment a pointer or index to the next element in an array (pointer arithmetic), as well as "end of loop" tests. If an optimizing compiler or assembler is able to pre-calculate offsets to each individually referenced array variable, these can be built into the machine code instructions directly, therefore requiring no additional arithmetic operations at run time (note that in the example given below this is not the case).
- Significant gains can be realized if the reduction in executed instructions more than compensates for any performance reduction caused by any increase in the size of the program.
- branch penalty is minimised.
- If the statements in the loop are independent of each other (i.e. where statements that occur earlier in the loop do not affect statements that follow them), the statements can potentially be executed in parallel.
- Can be implemented dynamically if the number of array elements is unknown at compile time
Optimizing compilers will sometimes perform the unrolling automatically or upon request.
Read more about this topic: Loop Unwinding
Famous quotes containing the word advantages:
“Can you conceive what it is to native-born American women citizens, accustomed to the advantages of our schools, our churches and the mingling of our social life, to ask over and over again for so simple a thing as that we, the people, should mean women as well as men; that our Constitution should mean exactly what it says?”
—Mary F. Eastman, U.S. suffragist. As quoted in History of Woman Suffrage, vol. 4 ch. 5, by Susan B. Anthony and Ida Husted Harper (1902)
“... there are no chains so galling as the chains of ignoranceno fetters so binding as those that bind the soul, and exclude it from the vast field of useful and scientific knowledge. O, had I received the advantages of early education, my ideas would, ere now, have expanded far and wide; but, alas! I possess nothing but moral capabilityno teachings but the teachings of the Holy Spirit.”
—Maria Stewart (18031879)
“No advantages in this world are pure and unmixed.”
—David Hume (17111776)