coder.unroll
Unrollfor
-loop by making a copy of the loop body for each loop iteration
Syntax
Description
coder.unroll()
unrolls afor
循环。Thecoder.unroll
call must be on a line by itself immediately preceding thefor
-loop that it unrolls.
而不是生产for
-loop in the generated code, loop unrolling produces a copy of thefor
-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 thefor-
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
循环。如果启发式足总il 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
. SeeNonconstant 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
循环。The default value of the threshold is5
. By modifying this threshold, you can fine-tune loop unrolling. For more details, seeUnroll for-Loops and parfor-Loops.