Main Content

MATLABCoderOptimizations in Generated Code

To improve the execution speed and memory usage of generated code,MATLAB®Coder™introduces the following optimizations:

Constant Folding

When possible, the code generator evaluates expressions in your MATLAB code that involve compile-time constants only. In the generated code, it replaces these expressions with the result of the evaluations. This behavior is known as constant folding. Because of constant folding, the generated code does not have to evaluate the constants during execution.

The following example shows MATLAB code that is constant-folded during code generation. The functionMultiplyConstantmultiplies every element in a matrix by a scalar constant. The function evaluates this constant using the product of three compile-time constants,a,b, andc.

functionout=MultiplyConstant(in)%#codegena=pi^4; b=1/factorial(4); c=exp(-1); out=in.*(a*b*c);end

The code generator evaluates the expressions involving compile-time constants,a,b, andc. It replaces these expressions with the result of the evaluation in generated code.

Constant folding can occur when the expressions involve scalars only. To explicitly enforce constant folding of expressions in other cases, use thecoder.constfunction. For more information, seeFold Function Calls into Constants.

Control Constant Folding

You can control the maximum number of instructions that can be constant-folded from the command line or the project settings dialog box.

  • At the command line, create a configuration object for code generation. Set the propertyConstantFoldingTimeoutto the value that you want.

    cfg=coder.config('lib'); cfg.ConstantFoldingTimeout = 200;
  • Using the app, in the project settings dialog box, on theAll Settingstab, set the fieldConstant folding timeoutto the value that you want.

Loop Fusion

在可能的情况下,代码生成器引线连续loops with the same number of runs into a single loop in the generated code. This optimization reduces loop overhead.

The following code contains successive loops, which are fused during code generation. The functionSumAndProductevaluates the sum and product of the elements in an arrayArr. The function uses two separate loops to evaluate the sumy_f_sumand producty_f_prod.

function[y_f_sum,y_f_prod] = SumAndProduct(Arr)%#codegeny_f_sum = 0; y_f_prod = 1;fori = 1:length(Arr) y_f_sum = y_f_sum+Arr(i);endfori = 1:length(Arr) y_f_prod = y_f_prod*Arr(i);end

The code generated from this MATLAB code evaluates the sum and product in a single loop.

Successive Matrix Operations Combined

When possible, the code generator converts successive matrix operations in your MATLAB code into a single loop operation in generated code. This optimization reduces excess loop overhead involved in performing the matrix operations in separate loops.

The following example contains code where successive matrix operations take place. The functionManipulateMatrixmultiplies every element of a matrixMatwith afactor. To every element in the result, the function then adds a转变:

functionRes=ManipulateMatrix(Mat,factor,shift) Res=Mat*factor; Res=Res+shift;end

生成的code combines the multiplication and addition into a single loop operation.

Unreachable Code Elimination

When possible, the code generator suppresses code generation from unreachable procedures in your MATLAB code. For instance, if a branch of anif, elseif, elsestatement is unreachable, then code is not generated for that branch.

The following example contains unreachable code, which is eliminated during code generation. The functionSaturateValuereturns a value based on the range of its inputx.

functiony_b = SaturateValue(x)%#codegenifx>0 y_b = x;elseifx>10%This is redundanty_b = 10;elsey_b = -x;end

The second branch of theif,elseif,elsestatement is unreachable. If the variablexis greater than 10, it is also greater than 0. Therefore, the first branch is executed in preference to the second branch.

MATLAB Coderdoes not generate code for the unreachable second branch.

memcpyCalls

To optimize generated code that copies consecutive array elements, the code generator tries to replace the code with amemcpycall. Amemcpycall can be more efficient than code, such as afor-loop or multiple, consecutive element assignments.

Seememcpy Optimization.

memsetCalls

To optimize generated code that assigns a literal constant to consecutive array elements, the code generator tries to replace the code with amemsetcall. Amemsetcall can be more efficient than code, such as afor-loop or multiple, consecutive element assignments.

Seememset Optimization.