Main Content

Memory Usage

Reduce RAM, ROM, and stack space used by generated C/C++ code

Determine how to modify your MATLAB® code so that the generated code uses less memory. When calling functions, control how data is passed by using the same variables for input and output. Passing by reference reduces the memory used in generated code. Control how memory is allocated by setting limits for stack space usage and by specifying when dynamic memory allocation is used. Minimize code size by disabling features that generate additional code, such as support for integer overflow.

For more information about how to optimize your code for specific conditions, see Optimization Strategies.


coder.inlineControl inlining of a specific function in generated code
coder.constFold expressions into constants in generated code
coder.cevalCall external C/C++ function


coder.ConstantSpecification of constant value for code generation
coder.ExternalDependencyInterface to external code


Generated Code Optimizations

Optimization Strategies

Optimize the execution speed or memory usage of generated code.

Stack Allocation and Performance

Allocate large variables on the heap when you have limited stack space.

MATLAB Coder Optimizations in Generated Code

To improve the performance of generated code, the code generator uses optimizations.

Conditional Statements

Prevent Code Generation for Unused Execution Paths

Make a control-flow variable constant to prevent code generation of unused branches.

Excluding Unused Paths from Generated Code

Make the control-flow variable constant to prevent generation of code for unused branches.

Function Calls

Avoid Data Copies of Function Inputs in Generated Code

Generate code that passes input arguments by reference.

Control Inlining to Fine-Tune Performance and Readability of Generated Code

Inlining eliminates the overhead of function calls but can produce larger C/C++ code and reduce code readability.

Control Stack Space Usage

Specify the maximum stack space that the generated code can use.

Fold Function Calls into Constants

Reduce execution time by replacing expression with constant in the generated code.


Set Dynamic Memory Allocation Threshold

Disable dynamic memory allocation for arrays less than a certain size.

Reuse Large Arrays and Structures

Specify variable reuse to reduce memory usage.

Code Generation for Sparse Matrices

Use sparse matrices in MATLAB code intended for code generation.

Numerical Edge Cases

Disable Support for Integer Overflow or Nonfinites

Improve performance by suppressing generation of supporting code to handle integer overflow or nonfinites.

Custom Code Integration

Integrate External/Custom Code

Improve performance by integrating your own optimized code.

Optimize Generated Code for Fast Fourier Transform Functions

Choose the correct fast Fourier transform implementation for your workflow and target hardware.

Speed Up Linear Algebra in Generated Standalone Code by Using LAPACK Calls

Generate LAPACK calls for certain linear algebra functions. Specify LAPACK library to use.

Speed Up Matrix Operations in Generated Standalone Code by Using BLAS Calls

Generate BLAS calls for certain low-level matrix operations. Specify BLAS library to use.

Speed Up Fast Fourier Transforms in Generated Standalone Code by Using FFTW Library Calls

Generate FFTW library calls for fast Fourier transforms. Specify the FFTW library.