I want know what the fastest function for copy n bytes from *source to *destination, when
*destination = *source + k
, and k is netural or zero
.
The memcpy() function has undefind behavior for overlapping.
The memmove() function is not the best, since it has to check the overlapping side.
There is another function that is optimal to my case?
Thanks in advance!
memmove()
has to spend some time determining how and whether the source and target overlap, so it can decide the order in which to copy the data.
Hypothetically, a version of memmove()
that omits this initial calculation, either because it assumes a particular direction or because it has an extra parameter that lets you tell it, might be a little faster than memmove()
.
But there is no such function in the standard library.
You could write your own function, but it's unlikely that it would be as fast as your system's memmove()
, which is very likely to be heavily optimized. You could grab a copy of the source code for your system's memmove()
function (if it's available) and modify it, but the result would likely be non-portable (memmove()
can depend on the characteristics of the system it's running on).
But it's very unlikely that it would be worth the effort. The time memmove()
spends doing the initial calculation is, for moves of reasonable size, likely to be a small fraction of the time spent copying the data.
Unless you know from actual measurement that the initial computation performed by memmove()
carries a significant performance penalty for your program, just use memmove()
itself.