When analyzing the copy function in stl source code analysis, it is said that the fastest way is to call the memmove function
However, I have read the source code of memmove, and I feel it should be as fast as the copy function of RandomAccessIterator version.
Why do you say memmove is the fastest operation at the bottom?
void *memmove(void *dest, const void *src, size_t count)
{
assert(dest ! = NULL && src ! = NULL)
if (dest < src)
{
char *p = (char *)dest;
char *q = (char *)src;
while (count--)
{
*p++ = *q++;
}
}
else
{
char *p = (char *)dest + count;
char *q = (char *)src + count;
while (count--)
{
*--p = *--q;
}
}
return dest;
}
This is the copy function of RandomAccessIterator version
template <class RandomAccessIterator, class OutputIterator, class Distance>
inline OutputIterator
__copy_d(RandomAccessIterator first, RandomAccessIterator last,
OutputIterator result, Distance*)
{
for (Distance n = last - first; n > 0; --n, ++result, ++first)
*result = *first;
return result;
}
. . Successive byte copies are certainly much faster than iterators accessing assignments.