coder.unroll
Unroll for
-loop by making a copy of
the loop body for each loop iteration
Description
coder.unroll()
unrolls a for
-loop.
The coder.unroll
call must be on a line by itself
immediately preceding the for
-loop that it unrolls.
Instead of producing a for
-loop in the
generated code, loop unrolling produces a copy of the for
-loop
body for each loop iteration. In each iteration, the loop index becomes
constant. To unroll a loop, the code generator must be able to determine
the bounds of the for-
loop.
For small, tight loops, unrolling can improve performance. However, for large loops, unrolling can increase code generation time significantly and generate inefficient code.
coder.unroll
is ignored outside of code
generation.
Examples
Input Arguments
Tips
Sometimes, the code generator unrolls a
for
-loop even though you do not usecoder.unroll
. For example, if afor
-loop indexes into a heterogeneous cell array or intovarargin
orvarargout
, the code generator unrolls the loop. By unrolling the loop, the code generator can determine the value of the index for each loop iteration. The code generator uses heuristics to determine when to unroll afor
-loop. If the heuristics fail to identify that unrolling is warranted, or if the number of loop iterations exceeds a limit, code generation fails. In these cases, you can force loop unrolling by usingcoder.unroll
. See Nonconstant Index into varargin or varargout in a for-Loop.If a
for
-loop is not preceded bycoder.unroll
, the code generator uses a loop unrolling threshold to determine whether to automatically unroll the loop. If the number of loop iterations is less than the threshold, the code generator unrolls the loop. If the number of iterations is greater than or equal to the threshold, the code generator produces afor
-loop. The default value of the threshold is5
. By modifying this threshold, you can fine-tune loop unrolling. For more details, see Unroll for-Loops and parfor-Loops.
Extended Capabilities
Version History
Introduced in R2011a