主要内容

通过将布尔数据打包到位字段中对生成的代码进行优化

此示例说明如何通过将布尔数据打包到位字段中来对生成的代码进行优化。当您选择模型配置参数将布尔数据打包到位字段中时,Embedded Coder® 会将布尔信号打包到 1 位位字段中,从而减少 RAM 消耗量。默认情况下,此优化处于启用状态。此优化会减少 RAM 消耗量。请注意,此优化可能会增大代码大小和执行时间。

示例模型

以模型 PackBooleanData 为例。

model = 'PackBooleanData';
open_system(model);

禁用优化

  1. 打开“配置参数”对话框。

  2. 优化窗格上,清除将布尔数据打包到位字段中

您也可以使用命令行 API 禁用优化:

set_param(model,'BooleansAsBitfields','off');

生成代码但不进行优化

使用 Embedded Coder 编译模型。

slbuild(model)
### Searching for referenced models in model 'PackBooleanData'.
### Total of 1 models to build.
### Starting build procedure for: PackBooleanData
### Successful completion of build procedure for: PackBooleanData

Build Summary

Top model targets:

Model            Build Reason                                         Status                        Build Duration
==================================================================================================================
PackBooleanData  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 7.9334s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 8.3753s

查看生成的代码而不进行优化。这些代码行位于 PackBooleanData.h 中。

hfile = fullfile('PackBooleanData_ert_rtw','PackBooleanData.h');
coder.example.extractLines(hfile,'/* Block signals and states','/* External inputs',1,0);
/* Block signals and states (default storage) for system '<Root>' */
typedef struct {
  boolean_T LogicalOp1;                /* '<Root>/Logical Op1' */
  boolean_T LogicalOp2;                /* '<Root>/Logical Op2' */
  boolean_T LogicalOp5;                /* '<Root>/Logical Op5' */
  boolean_T LogicalOp3;                /* '<Root>/Logical Op3' */
  boolean_T LogicalOp4;                /* '<Root>/Logical Op4' */
  boolean_T RelationalOperator;        /* '<Root>/Relational Operator' */
  boolean_T UnitDelay_DSTATE;          /* '<Root>/Unit Delay' */
} DW;

启用优化

  1. 打开“配置参数”对话框。

  2. 优化窗格上,选择将布尔数据打包到位字段中

您也可以使用命令行 API 启用优化:

set_param(model,'BooleansAsBitfields','on');

生成代码并进行优化

使用 Embedded Coder 编译模型。

slbuild(model)
### Searching for referenced models in model 'PackBooleanData'.
### Total of 1 models to build.
### Starting build procedure for: PackBooleanData
### Successful completion of build procedure for: PackBooleanData

Build Summary

Top model targets:

Model            Build Reason                     Status                        Build Duration
==============================================================================================
PackBooleanData  Generated code was out of date.  Code generated and compiled.  0h 0m 7.1486s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 7.618s

查看优化后的生成代码。这些代码行位于 PackBooleanData.h 中。

hfile = fullfile('PackBooleanData_ert_rtw','PackBooleanData.h');
coder.example.extractLines(hfile,'/* Block signals and states','/* External inputs',1,0);
/* Block signals and states (default storage) for system '<Root>' */
typedef struct {
  struct {
    uint_T LogicalOp1 : 1;             /* '<Root>/Logical Op1' */
    uint_T LogicalOp2 : 1;             /* '<Root>/Logical Op2' */
    uint_T LogicalOp5 : 1;             /* '<Root>/Logical Op5' */
    uint_T LogicalOp3 : 1;             /* '<Root>/Logical Op3' */
    uint_T LogicalOp4 : 1;             /* '<Root>/Logical Op4' */
    uint_T RelationalOperator : 1;     /* '<Root>/Relational Operator' */
    uint_T UnitDelay_DSTATE : 1;       /* '<Root>/Unit Delay' */
  } bitsForTID0;
} DW;

选择将布尔数据打包到位字段中会启用模型配置参数位字段声明符类型设定符。要进一步优化您的代码,请选择 uchar_t。但是,位字段声明符类型设定符设置的优化好处取决于您选择的目标。

关闭模型和代码生成报告。

bdclose(model)

另请参阅

主题