memset Optimization
To optimize generated code that assigns a literal constant to consecutive array elements, the
code generator tries to replace the code with a memset
call. A
memset
call can be more efficient than a
for
-loop or multiple, consecutive element assignments. This table
shows examples of generated C code with and without memset
.
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 the memset
optimization for assignment
of an integer constant or a floating-point zero. The use of memset
depends on:
The size of the value to assign. The size must meet the requirements for a C/C++
memset
call.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.
The memset
optimization threshold is the same as the
memcpy
optimization threshold. The default threshold is 64
bytes. To change the threshold:
At the command line, set the code configuration object property
MemcpyThreshold
.In the MATLAB® Coder™ app, set Memcpy threshold (bytes).
For assignment of floating-point zero, to enable or disable the
memset
optimization:
At the command line, set the code configuration object property
InitFltsAndDblsToZero
totrue
orfalse
. The default value istrue
.In the MATLAB Coder app, set Use memset to initialize floats and doubles to 0.0 to
Yes
orNo
. The default value isYes
.