memset Optimization
To optimize generated code that assigns a literal constant to consecutive array elements, the code generator tries to replace the code with amemset
调用。一个memset
call can be more efficient than afor
-loop or multiple, consecutive element assignments. This table shows examples of generated C code with and withoutmemset
.
Code Generated with memset Optimization | Code Generated Without memset Optimization |
---|---|
memset(&Y[0], 125, 100U * sizeof(signed char)); |
for (i = 0; i < 100; i++) { Y[i] = 125; |
memset(&Z[0], 0, 1000U * sizeof(double)); |
Z[0] = 0.0; Z[1] = 0.0; Z[2] = 0.0; ... Z[999] = 0.0; |
The code generator can use thememset
optimization for assignment of an integer constant or a floating-point zero. The use ofmemset
depends on:
The size of the value to assign. The size must meet the requirements for a C/C++
memset
调用。The number of bytes to assign. The number of bytes to assign is the number of array elements multiplied by the number of bytes required for the C/C++ data type.
If the number of elements to assign is known at compile time, then the code generator produces a
memset
call only when the number of bytes is greater than or equal to the threshold.If the number of elements is not known at compile time, then the code generator produces a
memset
call without regard to the threshold.
Thememset
optimization threshold is the same as thememcpy
optimization threshold. The default threshold is 64 bytes. To change the threshold:
一个t the command line, set the code configuration object property
MemcpyThreshold
.In theMATLAB®Coder™app, setMemcpy threshold (bytes).
For assignment of floating-point zero, to enable or disable thememset
optimization:
一个t the command line, set the code configuration object property
InitFltsAndDblsToZero
totrue
orfalse
. The default value istrue
.In theMATLAB Coderapp, setUse memset to initialize floats and doubles to 0.0to
Yes
orNo
. The default value isYes
.