www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/EliminateZeroInitExample.m
%% Eliminate Zero Initialization Code for Internal Data % This example shows how to eliminate generated code that initializes internal % data with zeroes, for example global DWork vectors, to reduce the size of the % code and to accelerate model initialization. % Copyright 2009-2014 The MathWorks, Inc. %% Overview % During model initialization, generated code can initialize internal % data by using assignments to zero. DWork vectors are an example of % internal data. % % If the data are global variables in the generated % code, and if the target environment already initializes global % variables with zeroes, you can remove the corresponding lines of % model initialization code. % % This optimization removes unnecessary zero initialization code, providing % these benefits: % % * Reduction in size of generated code % * Increased execution speed of generated code %% Open Example Model % Open the model <matlab:rtwdemo_internal_init rtwdemo_internal_init>. % The model contains an enabled subsystem whose initial output is zero. % The subsystem contains a Unit Delay block whose initial condition is |0|. model = 'rtwdemo_internal_init'; open_system(model); %% Generate Code Without Optimization % Build the model using Embedded Coder. currentDir = pwd; [~,cgDir] = rtwdemodir(); rtwbuild(model); %% % View the following code from the generated file |rtwdemo_internal_init.c|. cfile = fullfile(cgDir,'rtwdemo_internal_init_ert_rtw','rtwdemo_internal_init.c'); rtwdemodbtype(cfile,'/* Model initialize', '* File trailer', 1, 0); %% Enable Optimization % Open the Configuration Parameters dialog box. On the *Optimization* pane, % select *Remove internal data zero initialization*. % % Alternatively, you can use the command prompt to enable the % optimization. To enable the optimization, set the model parameter % |ZeroInternalMemoryAtStartup| to |'off'|. % % set_param(model, 'ZeroInternalMemoryAtStartup', 'off'); set_param(model, 'ZeroInternalMemoryAtStartup', 'off'); %% Generate Code with Optimization % Build the model using Embedded Coder. rtwbuild(model); %% % View the following code from the file |rtwdemo_internal_init.c|. % The generated code does not initialize internal data by assignment to zero. rtwdemodbtype(cfile,'/* Model initialize', '* File trailer', 1, 0); bdclose(model) rtwdemoclean; cd(currentDir)