减少数据副本
通过消除生成的代码中的数据副本,减少 RAM 和 ROM 消耗并提高代码执行速度。代码生成器会消除一些数据副本。通过调整某些模型参数设置、为 MATLAB Function 模块的输入和输出参量指定相同的名称,以及使用 Simulink 信号指定重用,您可以消除其他数据副本。
模型设置
| 内联不变信号 | 在生成的代码中转换不变信号的符号名称 |
| 消除多余的局部变量(表达式折叠) | 指定将表达式折叠成单个表达式 |
| 用户函数之间的函数内联 | Control inlining for calls from user-written functions to other user-written functions (自 R2025a 起) |
| MathWorks 函数之间的函数内联 | Control inlining for calls from MathWorks functions to other MathWorks functions (自 R2025a 起) |
| 用户函数和 MathWorks 函数之间的函数内联 | Control inlining for calls from user-written functions to MathWorks functions and from MathWorks functions to user-written functions (自 R2025a 起) |
主题
- 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 Buses
Model buses to maximize efficiency of 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
forloops withmemcpyfunction calls. - 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.