Documentation

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调用。一个memsetcall 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 thememsetoptimization for assignment of an integer constant or a floating-point zero. The use ofmemsetdepends 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 amemsetcall 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 amemsetcall without regard to the threshold.

Thememsetoptimization threshold is the same as thememcpyoptimization threshold. The default threshold is 64 bytes. To change the threshold:

  • 一个t the command line, set the code configuration object propertyMemcpyThreshold.

  • In theMATLAB®Coder™app, setMemcpy threshold (bytes).

For assignment of floating-point zero, to enable or disable thememsetoptimization:

  • 一个t the command line, set the code configuration object propertyInitFltsAndDblsToZerototrueorfalse. The default value istrue.

  • In theMATLAB Coderapp, setUse memset to initialize floats and doubles to 0.0toYesorNo. The default value isYes.

Related Topics

Was this topic helpful?