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.

Functions

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

Classes

coder.ConstantRepresent set containing one MATLAB value
coder.ExternalDependencyInterface to external code

Examples and How To

Conditional Statements

Prevent Code Generation for Unused Execution Paths

Make a control-flow variable constant to prevent code generation of 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.

Arrays

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.

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.

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.

Concepts

Optimization Strategies

Optimize the execution speed or memory usage of generated code.

Excluding Unused Paths from Generated Code

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

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.

Disable Support for Integer Overflow or Nonfinites

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

Code Generation for Sparse Matrices

Use sparse matrices in MATLAB code intended for code generation.