memset Optimization - MATLAB & Simulink (original) (raw)

Main Content

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. Amemset call can be more efficient than afor-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 memset optimization threshold is the same as thememcpy optimization threshold. The default threshold is 64 bytes. To change the threshold:

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