主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

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

此示例显示如何通过将布尔数据打包到位域来优化生成的代码。当您选择模型配置参数将布尔数据打包到位域时,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 10.303s 

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

查看未经优化的生成代码。这些代码行在 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 9.542s  

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

查看优化后生成的代码。这些代码行在 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)

另请参阅

主题