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.
In the Command Window, type rtwdemo_reuse_global.
On the Configuration Parameters dialog box, 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.
Press Ctrl+B to generate code.
### Starting build procedure for: rtwdemo_reuse_global ### Successful completion of build procedure for: rtwdemo_reuse_global Build Summary Top model targets built: Model Action Rebuild Reason ===================================================================================================== rtwdemo_reuse_global 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 6.2978s
View the generated code without the optimization. Here is a portion of rtwdemo_reuse_global.c
.
/* Model step function */ void rtwdemo_reuse_global_step(void) { /* Sum: '<Root>/Sum' incorporates: * Delay: '<Root>/Delay' * Inport: '<Root>/In1' */ rtDW.Delay_DSTATE += rtU.In1; /* Outport: '<Root>/Out1' incorporates: * Delay: '<Root>/Delay' */ rtY.Out1 = rtDW.Delay_DSTATE; }
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.
On the Configuration Parameters dialog box, 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
.
### Starting build procedure for: rtwdemo_reuse_global ### Successful completion of build procedure for: rtwdemo_reuse_global Build Summary Top model targets built: Model Action Rebuild Reason ==================================================================================== rtwdemo_reuse_global 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 6.4308s /* Model step function */ void rtwdemo_reuse_global_step(void) { /* Sum: '<Root>/Sum' incorporates: * Delay: '<Root>/Delay' * Inport: '<Root>/In1' */ rtY.Out1 += rtU.In1; }
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.