This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

MATLAB Coder Optimizations 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 function MultiplyConstant multiplies every element in a matrix by a scalar constant. The function evaluates this constant using the product of three compile-time constants, a, b, and c.

function out=MultiplyConstant(in) %#codegen
 a=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, and c. 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 the coder.const function. For more information, see Fold 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 property ConstantFoldingTimeout to the value that you want.

    cfg=coder.config('lib');
    cfg.ConstantFoldingTimeout = 200;
  • Using the app, in the project settings dialog box, on the All Settings tab, set the field Constant folding timeout to the value that you want.

Loop Fusion

When possible, the code generator fuses successive 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 function SumAndProduct evaluates the sum and product of the elements in an array Arr. The function uses two separate loops to evaluate the sum y_f_sum and product y_f_prod.

function [y_f_sum,y_f_prod] = SumAndProduct(Arr) %#codegen
  y_f_sum = 0;
  y_f_prod = 1;
  for i = 1:length(Arr)
     y_f_sum = y_f_sum+Arr(i);
  end
  for i = 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 function ManipulateMatrix multiplies every element of a matrix Mat with a factor. To every element in the result, the function then adds a shift:

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

The generated 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 an if, elseif, else statement is unreachable, then code is not generated for that branch.

The following example contains unreachable code, which is eliminated during code generation. The function SaturateValue returns a value based on the range of its input x.

function y_b = SaturateValue(x) %#codegen
  if x>0
    y_b = x;
  elseif x>10 %This is redundant
    y_b = 10;
  else
    y_b = -x;
 end

The second branch of the if, elseif, else statement is unreachable. If the variable x is greater than 10, it is also greater than 0. Therefore, the first branch is executed in preference to the second branch.

MATLAB Coder does not generate code for the unreachable second branch.

memcpy Calls

To optimize generated code that copies consecutive array elements, the code generator tries to replace the code with a memcpy call. A memcpy call can be more efficient than code, such as a for-loop or multiple, consecutive element assignments.

See memcpy Optimization.

memset Calls

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

See memset Optimization.