Main Content

memcpy Optimization

To optimize generated code that copies consecutive array elements, the code generator tries to replace the code with amemcpy调用。一个memcpycall can be more efficient than afor-loop or multiple, consecutive element assignments. This table shows examples of generated C code with and without thememcpyoptimization.

Code Generated with memcpy Optimization Code Generated Without memcpy Optimization
memcpy(&C[0], &A[0], 10000U * sizeof(double));
for (i0 = 0; i0 < 10000; i0++) { C[i0] = A[i0];
memcpy(&Z[0], &X[0],1000U * sizeof(double));
Z[0] = X[0]; Z[1] = X[1]; Z[2] = X[2]; ... Z[999] = X[999];

启用或禁用memcpyoptimization:

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

  • In theMATLAB®Coder™app, setUse memcpy for vector assignmenttoYesorNo. The default value isYes.

When thememcpyoptimization is enabled, the use ofmemcpydepends on the number of bytes to copy. The number of bytes to copy 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 copy is known at compile time, then the code generator produces amemcpy称只有当the number of bytes is greater than or equal to thememcpythreshold.

  • If the number of elements is not known at compile time, then the code generator produces amemcpycall without regard to the threshold.

The defaultmemcpythreshold is 64 bytes. To change the threshold:

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

  • In theMATLAB Coderapp, setMemcpy threshold (bytes).

Thememsetoptimization also uses thememcpythreshold.

In certain cases, the code generator can produce amemcpycall without regard to theEnableMemcpyorMemcpyThresholdparameters, or their equivalent settings in the app.

Related Topics