www.gusucode.com > ecoder 案例源码程序 matlab代码 > ecoder/UseGlobalToHoldTemporaryResultsExample.m
%% Use Global to Hold Temporary Results % The code generator uses global and local variables % when you select |None| versus when you select |Use global to hold temporary| % |results|. %% Example Model % In the model <matlab:rtwdemo_optimize_global_ebf>, an Assignment block assigns values coming from the Inport and Constant % blocks to an output signal. The output signal feeds into a Gain block. % % <<../rtwdemo_optimize_global_ebf.png>> % model = 'rtwdemo_optimize_global_ebf'; load_system('rtwdemo_optimize_global_ebf') %% Generate Code without Optimization % % # In the Configuration Parameters dialog box, on the All Parameters tab, % verify that the *Signal storage reuse* parameter is selected. % # In the Configuration Parameters dialog box, for the *Optimize global % access parameter*, select |None| or enter the following command in the % MATLAB Command Window: set_param('rtwdemo_optimize_global_ebf','GlobalVariableUsage','None'); %% % In your system's temporary folder, create a folder for the % build and inspection process: currentDir = pwd; [~,cgDir] = rtwdemodir(); %% % Build the model. rtwbuild(model); %% % View the generated code without the optimization. Here is a portion of % |rtwdemo_optimize_global_ebf.c|. cfile = fullfile(cgDir,'rtwdemo_optimize_global_ebf_ert_rtw',... 'rtwdemo_optimize_global_ebf.c'); rtwdemodbtype(cfile,'/* Model step','/* Model initialize',1, 0); %% % The code assigns values to the local vector |rtb_Assignment|. The last % statement copies the values in the local vector |rtb_Assignment| to the % global vector |rtY.Out1|. Fewer global variable references result in % improved execution speed. The code uses more instructions for global % variable references than for local variable references. %% % In the Static Code Metrics Report, examine the Global Variables section. %% % # In the Code Generation Report window, select *Static Code Metrics Report*. % # Scroll down to the |Global Variables| section. % # Select the *[+]* sign before each variable to expand it. % % <<../global_low.png>> % %% % The total number of reads and writes for global variables is 2. %% Generate Code with Optimization % % In the Configuration Parameters dialog box, for the *Optimize global % access parameter*, select |Use global to hold temporary results|, or % enter the following command in the MATLAB Command Window: set_param('rtwdemo_optimize_global_ebf',... 'GlobalVariableUsage','Use global to hold temporary results'); %% % Build the model. rtwbuild(model); %% % View the generated code with the optimization. Here is a portion of % |rtwdemo_optimize_global_ebf.c|. cfile = fullfile(cgDir,'rtwdemo_optimize_global_ebf_ert_rtw',... 'rtwdemo_optimize_global_ebf.c'); rtwdemodbtype(cfile,'/* Model step','/* Model initialize',1, 0); %% % The code assigns values to the global vector |rtY.Out1| without using a % local variable. This assignment improves ROM and RAM consumption and % reduces data copies. The code places the value in the destination % variable for each assignment instead of copying the value at the end. % In the Static Code Metrics Report, examine the Global Variables section. % % <<../global_max.png>> % % As a result of using global variables to hold local results, the total % number of reads and writes for global variables has increased from 2 to % 5. This optimization reduces data copies by reusing global variables. %% % Close the code generation report. rtwdemoclean; cd(currentDir)