Optimization Strategies
MATLAB®
Coder™ introduces certain optimizations when generating C/C++
code
or MEX
functions from your MATLAB code. For
more information, see MATLAB Coder Optimizations in Generated Code.
To optimize your generated code further, you can:
Adapt your MATLAB code.
Control code generation using the configuration object from the command-line or the project settings dialog box.
To optimize the execution speed of generated code, for these conditions, perform the following actions as necessary:
Condition | Action |
---|---|
You have for -loops whose iterations are
independent of each other. | |
You have variable-size arrays in your MATLAB code. | Minimize Dynamic Memory Allocation |
You have multiple variable-size arrays in your MATLAB code. You want dynamic memory allocation for larger arrays and static allocation for smaller ones. | Set Dynamic Memory Allocation Threshold |
You want your generated function to be called by reference. | Avoid Data Copies of Function Inputs in Generated Code |
You are calling small functions in your MATLAB code. | Inline small functions using coder.inline("always") or coder.inlineCall . See Control Inlining to Fine-Tune Performance and Readability of Generated Code |
You have limited target memory for your generated code. You want to inline small functions and generate separate code for larger ones. | Control Inlining to Fine-Tune Performance and Readability of Generated Code |
You do not want to generate code for expressions that contain constants only. | Fold Function Calls into Constants |
You have loop operations in your MATLAB code that do not depend on the loop index. | Minimize Redundant Operations in Loops |
You have integer operations in your MATLAB code. You know beforehand that integer overflow does not occur during execution of your generated code. | Disable Support for Integer Overflow |
You know beforehand that Inf s and NaN s
do not occur during execution of your generated code. | Disable Support for Nonfinite Numbers |
You have for -loops with few iterations. | Unroll for-Loops and parfor-Loops |
You already have legacy C/C++ code optimized for your target environment. | Integrate External/Custom Code |
You want to speed up the code generated for basic vector and matrix functions. | Speed Up Matrix Operations in Generated Standalone Code by Using BLAS Calls |
You want to speed up the code generated for linear algebra functions. | Speed Up Linear Algebra in Generated Standalone Code by Using LAPACK Calls |
You want to speed up the code generated for fast Fourier transform (FFT) functions. | Speed Up Fast Fourier Transforms in Generated Standalone Code by Using FFTW Library Calls |
To optimize the memory usage of generated code, for these conditions, perform the following actions as necessary:
Condition | Action |
---|---|
You have if/else/elseif statements or switch/case/otherwise statements
in your MATLAB code. You do not require some branches of the
statements in your generated code. | Prevent Code Generation for Unused Execution Paths |
You want your generated function to be called by reference. | Avoid Data Copies of Function Inputs in Generated Code |
You have limited stack space for your generated code. | Control Stack Space Usage |
You are calling small functions in your MATLAB code. | Inline small functions using coder.inline("always") or coder.inlineCall . See Control Inlining to Fine-Tune Performance and Readability of Generated Code |
You have limited target memory for your generated code. You want to inline small functions and generate separate code for larger ones. | Control Inlining to Fine-Tune Performance and Readability of Generated Code |
You do not want to generate code for expressions that contain constants only. | Fold Function Calls into Constants |
You have loop operations in your MATLAB code that do not depend on the loop index. | Minimize Redundant Operations in Loops |
You have integer operations in your MATLAB code. You know beforehand that integer overflow does not occur during execution of your generated code. | Disable Support for Integer Overflow |
You know beforehand that Inf -s and NaN -s
does not occur during execution of your generated code. | Disable Support for Nonfinite Numbers |
Your MATLAB code has variables that are large arrays or structures. Your variables are not reused in the generated code. They are preserved. You want to see if the extra memory required to preserve the variable names of the large arrays or structures affects performance. | Reuse Large Arrays and Structures |