Optimize Generated C/C++ and MEX Code
By default, MATLAB® Coder™ introduces 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.
Code optimization is an optional step in the Generate C/C++ Code From MATLAB Code Using MATLAB Coder.
Strategies to Optimize Speed
Use this table to determine how to optimize the execution speed of the generated code.
Condition | Action |
---|---|
Your code has 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 to pass variables in the generated code by reference. | Avoid Data Copies of Function Inputs in Generated Code |
You want to maximize speed at the expense of memory usage by inlining most or all functions. | Control Inlining to Fine-Tune Performance and Readability of Generated Code |
You do not want to generate code for expressions that only contain constants. | Fold Function Calls into Constants in Generated Code |
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 and you know that integer overflow does not occur during execution of the generated code. | Disable Support for Integer Overflow |
You know that the generated code does not use nonfinite numbers | Disable Support for Nonfinite Numbers |
Your code has 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 |
Strategies to Optimize Memory Usage
Use this table to determine how to optimize the memory usage of the generated code.
Condition | Action |
---|---|
Your MATLAB code uses conditional statements, but you do not require some branches of these statements in the generated code. | Prevent Code Generation for Unused Execution Paths |
You want to pass variables in the generated code by reference. | Avoid Data Copies of Function Inputs in Generated Code |
You have limited stack space on the code generation target. | Control Stack Space Usage |
Because you have limited memory on the code generation target, you want to avoid inlining functions with many variables. | Control Inlining to Fine-Tune Performance and Readability of Generated Code |
You do not want to generate code for expressions that only contain constants. | Fold Function Calls into Constants in Generated Code |
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 and you know that integer overflow does not occur during execution of the generated code. | Disable Support for Integer Overflow |
You know that the generated code does not use nonfinite numbers | Disable Support for Nonfinite Numbers |
You want to optimize memory usage for variables that are large structures or arrays, even if this makes your code less readable. | Reuse Large Arrays and Structures |