Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Loop unrolling
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Simple manual example in C=== A procedure in a computer program is to delete 100 items from a collection. This is normally accomplished by means of a <code>''for''</code>-loop which calls the function ''delete(item_number)''. If this part of the program is to be optimized, and the overhead of the loop requires significant resources compared to those for the ''delete(x)'' function, unwinding can be used to speed it up. {| class="wikitable" !Normal loop !After loop unrolling |-style="vertical-align:top" | <syntaxhighlight lang="c"> int x; for (x = 0; x < 100; x++) { delete(x); } </syntaxhighlight> | <syntaxhighlight lang="c"> int x; for (x = 0; x < 100; x += 5) { delete(x); delete(x + 1); delete(x + 2); delete(x + 3); delete(x + 4); } </syntaxhighlight> |} As a result of this modification, the new program has to make only 20 iterations, instead of 100. Afterwards, only 20% of the jumps and conditional branches need to be taken, and represents, over many iterations, a potentially significant decrease in the loop administration overhead. To produce the optimal benefit, no variables should be specified in the unrolled code that require [[pointer arithmetic]]. This usually requires "[[Base address|base]] plus offset" addressing, rather than indexed referencing. On the other hand, this ''manual'' loop unrolling expands the source code size from 3 lines to 7, that have to be produced, checked, and debugged, and the compiler may have to allocate more registers to store variables in the expanded loop iteration {{Dubious|date=December 2009}}. In addition, the loop control variables and number of operations inside the unrolled loop structure have to be chosen carefully so that the result is indeed the same as in the original code (assuming this is a later optimization on already working code). For example, consider the implications if the iteration count were not divisible by 5. The manual amendments required also become somewhat more complicated if the test conditions are variables. See also [[Duff's device]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)