Optimize Generated Code Using memset
Function
This example shows how to optimize the generated code by using the memset
function to clear the internal storage. When you select the model configuration parameter Use memset to initialize floats and doubles to 0.0, the memset
function clears internal storage, to the integer bit pattern 0
(that is, all bits are off).
If your compiler and target CPU both represent floating-point zero with the integer bit pattern 0
, consider setting this parameter to gain execution and ROM efficiency.
NOTE: The command-line values are the reverse of the settings values. 'on' in the command line corresponds to clearing the setting. 'off'
in the command line corresponds to selecting the setting.
This optimization:
Reduces ROM consumption.
Improves execution speed.
Example Model
Consider the model MemsetOptimization
.
model = 'MemsetOptimization';
open_system(model);
Generate Code
The code generator uses a loop to initialize the Constant
block values. Build the model.
slbuild(model)
### Starting build procedure for: MemsetOptimization ### Successful completion of build procedure for: MemsetOptimization Build Summary Top model targets: Model Build Reason Status Build Duration ===================================================================================================================== MemsetOptimization Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 12.211s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 13.012s
View the generated code without the optimization. These lines of code are in MemsetOptimization.c
.
cfile = fullfile('MemsetOptimization_grt_rtw','MemsetOptimization.c'); coder.example.extractLines(cfile,'/* Model initialize function */',... '/* Model terminate function */',1,0);
/* Model initialize function */ void MemsetOptimization_initialize(void) { /* Registration code */ /* initialize error status */ rtmSetErrorStatus(MemsetOptimization_M, (NULL)); /* external outputs */ { int32_T i; for (i = 0; i < 50; i++) { MemsetOptimization_Y.Out1[i] = 0.0; } } { int32_T i; /* ConstCode for Outport: '<Root>/Out1' */ for (i = 0; i < 50; i++) { MemsetOptimization_Y.Out1[i] = 56.0; } /* End of ConstCode for Outport: '<Root>/Out1' */ } }
Enable Optimization
Open the Configuration Parameters dialog box.
In the Configuration Parameter dialog box select the Use memset to initialize floats and doubles to 0.0 parameter. Alternatively, you can use the command-line API to enable the optimization:
set_param(model,'InitFltsAndDblsToZero','off');
Generate Code with Optimization
The code generator uses the memset
function to initialize the Constant
block values.
Build the model.
slbuild(model)
### Starting build procedure for: MemsetOptimization ### Successful completion of build procedure for: MemsetOptimization Build Summary Top model targets: Model Build Reason Status Build Duration ================================================================================================= MemsetOptimization Generated code was out of date. Code generated and compiled. 0h 0m 7.8242s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 8.7211s
View the generated code with the optimization. These lines of code are in MemsetOptimization.c
.
coder.example.extractLines(cfile,'/* Model initialize function */',... '/* Model terminate function */',1,0);
/* Model initialize function */ void MemsetOptimization_initialize(void) { /* Registration code */ /* initialize error status */ rtmSetErrorStatus(MemsetOptimization_M, (NULL)); /* external outputs */ (void)memset(&MemsetOptimization_Y, 0, sizeof(ExtY_MemsetOptimization_T)); { int32_T i; /* ConstCode for Outport: '<Root>/Out1' */ for (i = 0; i < 50; i++) { MemsetOptimization_Y.Out1[i] = 56.0; } /* End of ConstCode for Outport: '<Root>/Out1' */ } }
See Also
Use memset to initialize floats and doubles to 0.0