Performance: memset

As others already pointed out, Linux uses an optimistic memory allocation strategy.

The difference between the first and the following memcpys is the initialization of DataDest.

As you have already seen, when you eliminate memset(DataSrc, 0, N), the first memcpy is even slower, because the pages for the source must be allocated as well. When you initialize both, DataSrc and DataDest, e.g.

memset(DataSrc, 0, N);
memset(DataDest, 0, N);

all memcpys will run with roughly the same speed.

For the second question: when you initialize the allocated memory with memset all pages will be laid out consecutively. On the other side, when the memory is allocated as you copy, the source and destination pages will be allocated interleaved, which might make the difference.

Leave a Comment