Main Content

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 OptimizationCode 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, use one of these approaches:

  • In a code configuration object, specify a value for the MemcpyThreshold property.

  • In the Code Generation Settings dialog box, specify a value for the Memcpy threshold (bytes) parameter.

By default, the code generator uses the memset optimization for assignment of float and double zero. To disable the memset optimization, use one of these approaches:

See Also

Topics