Main Content

このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。

計算結果に影響しないブロックのコードの削除

以下の例では、コード ジェネレーターを使用して、計算結果に影響しないコードを削除して生成されたコードを最適化する方法を示します。最適化により、以下が実現されます。

  • 実行速度が上がる。

  • ROM の消費を低減する。

モデルrtwdemo_blockreductionでは、値1.0の Gain ブロックは Inport ブロックと Outport ブロックの間にあります。

model ='rtwdemo_blockreduction'; open_system(model);

コードの生成

ビルドと検査プロセス用に一時フォルダーを作成します。

currentDir=pwd; [~,cgDir]=rtwdemodir();

モデルを作成します。

set_param(model,'BlockReduction','off'); slbuild(model)
### Starting build procedure for: rtwdemo_blockreduction ### Successful completion of build procedure for: rtwdemo_blockreduction Build Summary Top model targets built: Model Action Rebuild Reason ======================================================================================================= rtwdemo_blockreduction Code generated and compiled Code generation information file does not exist. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 15.376s

rtwdemo_blockreduction.cからのコードは、以下のとおりです。

cfile = fullfile(cgDir,'rtwdemo_blockreduction_ert_rtw','rtwdemo_blockreduction.c'); rtwdemodbtype(cfile,'/* Model step function */',...'/* Model initialize function */', 1, 0);
/* Model step function */ void rtwdemo_blockreduction_step(void) { /* Outport: '/Out1' incorporates: * Gain: '/Gain' * Inport: '/In1' */ rtwdemo_blockreduction_Y.Out1 = 1.0 * rtwdemo_blockreduction_U.In1; }

最適化の有効化

  1. [コンフィギュレーション パラメーター] ダイアログ ボックスを開きます。

  2. [ブロック削減]を選択します。既定の設定では、最適化はオンです。

代わりに、コマンド ライン API を使用して最適化を有効にします。

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

最適化を使用したコードの生成

slbuild(model)
### Starting build procedure for: rtwdemo_blockreduction ### Successful completion of build procedure for: rtwdemo_blockreduction Build Summary Top model targets built: Model Action Rebuild Reason ====================================================================================== rtwdemo_blockreduction Code generated and compiled Generated code was out of date. 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 10.748s

最適化されたrtwdemo_blockreduction.cからのコードは、以下のとおりです。

cfile = fullfile(cgDir,'rtwdemo_blockreduction_ert_rtw','rtwdemo_blockreduction.c'); rtwdemodbtype(cfile,'/* Model step function */',...'/* Model initialize function */', 1, 0);
/* Model step function */ void rtwdemo_blockreduction_step(void) { /* Outport: '/Out1' incorporates: * Inport: '/In1' */ rtwdemo_blockreduction_Y.Out1 = rtwdemo_blockreduction_U.In1; }

入力信号を1.0の値で乗算しても計算結果に影響しないため、コード ジェネレーターは Gain ブロックを生成されたコードから除外します。モデルを閉じてクリーンアップします。

bdclose(model) rtwdemoclean; cd(currentDir)

参考

関連するトピック