www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/ReuseGlobalBlockOutputsInTheGeneratedCodeExample.m

    %% Reuse Global Block Outputs in the Generated Code
% Reduce ROM and RAM consumption and data copies and increase execution speed of 
% generated code. Configure the code generator to reuse global variables by selecting 
% the model configuration parameter *Reuse global block outputs*.  
% 
% 
%% Example
% In the Command Window, type <matlab: rtwdemo_reuse_global>.
%
% <<../opt_signalsparams_reuse_block_outputs_model.png>>
%
%%
model='rtwdemo_reuse_global';
load_system('rtwdemo_reuse_global')
set_param(model,'GlobalBufferReuse', 'off'); 
%% Generate Code without Optimization
% 
% # In the Configuration Parameters dialog box, on the *All Parameters* tab,
% verify that *Signal storage reuse* is selected.
% # Clear *Reuse global block outputs* and click *Apply*.
% # On the *Code Generation > Report* pane, select *Static code metrics*.
% # In your system's temporary folder, create a folder for the build and
% inspection process.

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

%% 
% Press *Ctrl+B* to generate code.
rtwbuild(model);
%%
% View the generated code without the optimization. Here is a portion of
% |rtwdemo_reuse_global.c|.
cfile = fullfile(cgDir,'rtwdemo_reuse_global_ert_rtw','rtwdemo_reuse_global.c');
rtwdemodbtype(cfile,'/* Model step','/* Model initialize',1, 0);
%%
% The generated code contains a data copy to the global variable
% |rtDW.Delay_DSTATE|. Open the Static Code Metrics Report. The total
% number of reads and writes for global variables is 8. The total size is
% 32 bytes.
%% Enable Optimization and Generate Code
% 
% # On the *All Parameters* tab, select *Reuse global block outputs* and click *Apply*.
% # Generate code.
% # View the generated code with the optimization. Here is a portion of
% |rtwdemo_reuse_global.c|.
%
set_param(model,'GlobalBufferReuse', 'on');
rtwbuild(model);
cfile = fullfile(cgDir,'rtwdemo_reuse_global_ert_rtw','rtwdemo_reuse_global.c');
rtwdemodbtype(cfile,'/* Model step','/* Model initialize',1, 0);
%%
% The code generator eliminates a data copy, reduces two statements to one
% statement and three global variables to two global variables.
% 
% Open the Static Code Metrics Report. For global variables, this optimization
% reduces the total number of reads and writes for global variables from 8 
% to 5 and the total size from 32 bytes to 24 bytes.
%%
bdclose(model)
rtwdemoclean;
cd(currentDir)