性能
减少内存使用量并提高生成的代码的执行速度
当开发您打算从中生成代码的 Simulink® 模型时,请使用模型配置参数来减少 RAM 和 ROM 的消耗并加快代码执行速度。如果您愿意为提高代码的安全性而牺牲一定的效率,则可以禁用其中一个或多个参数。
您可以配置对代码的执行进行探查,以确定生成的代码是否符合需要改进内存使用量或执行速度的代码节和目标硬件的实时要求。您还可以使用代码生成顾问检查模型的当前配置是否已针对调试或代码执行进行了优化。
模型设置
主题
代码优化基础知识
- Increase Code Generation Speed
Alter a model to increase the speed at which the code generator generates code. - Design Techniques to Optimize Models for Efficient Code Generation
Apply design tools and techniques to optimize a model for code generation. - Control Compiler Optimizations
Control compiler optimizations for your makefile at the Simulink UI level.
防御式编程
- Subnormal Number Execution Speed
Minimize the possibility of execution slowdowns or overruns due to subnormal number calculation latency. - Remove Code for Out-of-Range Floating Point to Integer Conversions
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 ofNaN
do not exist in your application, specify that the code generator remove code that mapsNaN
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.
减少数据副本
- 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 replacingfor
loops withmemcpy
function calls. - Generate Efficient Code for Bus Signals
Model buses to maximize efficiency of the generated code.
内存使用量
- 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 andBoolean
variables. - Optimize Generated Code Using memset Function
Thememset
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 afor
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 asBoolean
data types. - 优化时间计数器的内存使用量
优化代码生成器为经过时间或绝对时间分配的内存。
执行速度
- Optimize Generated Code by Combining Multiple for Constructs
The code generator uses data dependency analysis to combinefor
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 afor
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. - 控制 MATLAB Function 模块中可变大小数组的内存分配
禁用动态内存分配或为 MATLAB Function 模块指定动态内存分配阈值。 - Generate SIMD Code from Simulink Blocks
Improve the execution speed of the generated code using Intel® SSE and Intel AVX technology. - Optimize Code for Reduction Operations by Using SIMD
Generate optimized code for reduction operations using SIMD instruction sets.
代码执行探查
- 生成代码的执行探查
使用代码执行探查来观察代码执行速度。