Performance

Reduce memory usage and improve execution speed of generated code

When developing Simulink® models from which you intend to generate code, use model configuration parameter to reduce RAM and ROM consumption and speed up code execution. If you want more defensive code, at the expense of some efficiency, you can disable one or more of these parameters.

You can configure code execution profiling to determine whether the generated code meets the real-time requirements of your target hardware and code sections that require memory usage or execution speed improvements. You can also use the Code Generation Advisor to check if a model’s current configuration is optimized for debugging or code execution.

Topics

Code Optimization Basics

Increase Code Generation Speed

Alter a model to increase the speed at which the code generator generates code.

Optimization Tools and Techniques

Apply tools, such as the Model Advisor, and techniques to optimize a model for code generation.

Control Compiler Optimizations

Control compiler optimizations for your makefile at the Simulink UI level.

Defensive Programming

Subnormal Number Execution Speed

Minimize the possibility of execution slowdowns or overruns due to subnormal number calculation latency.

Remove Code From Floating-Point to Integer Conversions That Wraps Out-of-Range Values

If the input values in your application are in the range of the output type, remove code for out-of-range floating-point to integer conversions.

Remove Code That Maps NaN to Integer Zero

If input values of NaN do not exist in your application, specify that the code generator remove code that maps NaN to integer zero.

Disable Nonfinite Checks or Inlining for Math Functions

Use code replacement library (CRL) customization entries to selectively disable nonfinite checks for math functions and inlining of math functions.

Data Copy Reduction

Minimize Computations and Storage for Intermediate Results at Block Outputs

The code generator folds block computations into a single expression, instead of generating separate code statements and storage declarations for each block in a model.

Fold Expressions

Expression fold code to minimize the computation of intermediate results at block outputs and the storage of such results in temporary buffers or variables.

Enable and Reuse Local Block Outputs in Generated Code

Where possible, the code generator declares block outputs as local variables, so that it can potentially reuse these variables.

Generate Efficient Code for Bus Signals

Model buses to maximize efficiency of the generated code.

Inline Invariant Signals

Use the numerical values of invariant signals instead of their symbolic names in the generated code.

Use memcpy Function to Optimize Generated Code for Vector Assignments

The code generator optimizes the generated code for vector assignments by replacing for loops with memcpy function calls.

Generate Efficient Code for Bus Signals

Model buses to maximize efficiency of the generated code.

Memory Usage

Vector Operation Optimization

The code generator reduces stack memory by replacing temporary local arrays with local variables.

Reduce Memory Usage for Boolean and State Configuration Variables

Reduce the amount of memory that stores state configuration variables and Boolean variables.

Optimize Generated Code Using memset Function

The memset function clears internal storage, regardless of type, to the integer bit pattern 0 (that is, all bits are off).

Customize Stack Space Allocation

Control the maximum allowable stack size to provide some control over whether data is defined locally or globally in the generated code.

Configure Loop Unrolling Threshold

Starting at a default value of 5, the code generator begins to use a for loop instead of separate statements to assign values to the elements of a signal or parameter array.

Specify Single-Precision Data Type for Embedded Application

Generate code whose floating-point data types are only single precision, for example, for targeting a single-precision processor.

Generate Efficient Code by Specifying Data Types for Block Parameters

To generate more efficient code, match parameter data types with signal data types or store parameters in smaller data types.

Optimize Generated Code Using Boolean Data for Logical Signals

The code generator optimizes the generated code by storing logical signals as Boolean data types.

Optimize Memory Usage for Time Counters

Optimize memory that the code generator allocates for elapsed time or absolute time.

Execution Speed

Optimize Generated Code by Combining Multiple for Constructs

The code generator uses data dependency analysis to combine for constructs to reduce static code size and runtime branching.

Configure Loop Unrolling Threshold

Starting at a default value of 5, the code generator begins to use a for loop instead of separate statements to assign values to the elements of a signal or parameter array.

Eliminate Dead Code Paths in Generated Code

The code generator eliminates dead (that is, unused) code paths from the generated code.

Floating-Point Multiplication to Handle a Net Slope Correction

For processors that support efficient multiplication, improve code efficiency by using floating-point multiplication to handle a net slope correction.

Remove Code for Blocks That Have No Effect on Computational Results

The code generator increases execution speed and reduces ROM consumption by removing code that does not impact computational results.

Inline Numeric Values of Block Parameters

Reduce global RAM usage by inlining the literal numeric values of block parameters.

Optimize Generated Code Using Fixed-Point Data with Simulink®, Stateflow®, and MATLAB®

Generate fixed-point code in Simulink, Stateflow®, and MATLAB®.

Generate Target Optimizations Within Algorithm Code

Customize generated algorithm code with target-specific optimizations.

Use Conditional Input Branch Execution

For Switch and Multiport Switch blocks, Simulink executes only blocks that compute the control input and the data input that the control input selects.

Optimize Generated Code for Complex Signals

The code generator performs various optimizations on the structures that represent signals in the generated code.

Speed Up Linear Algebra in Code Generated from a MATLAB Function Block

Generate LAPACK calls for certain linear algebra functions in a MATLAB function block. Specify LAPACK library to use.

Speed Up Matrix Operations in Code Generated from a MATLAB Function Block

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

Speed Up Fast Fourier Transforms in Code Generated from a MATLAB Function Block

Generate FFTW library calls for fast Fourier transforms in a MATLAB Function block. Specify the FFTW library.

Synchronize Multithreaded FFTW Planning in Code Generated from a MATLAB Function Block

Implement FFT library callback class methods and provide supporting C code to prevent concurrent access to FFTW planning.

Control Memory Allocation for Variable-Size Arrays in a MATLAB Function Block

Disable dynamic memory allocation or specify a dynamic memory allocation threshold for MATLAB Function blocks.

Code Execution Profiling

Execution Profiling for Generated Code

Use code execution profiling to observe code execution speed.