While you can emulate the function of memcpy
with simple assignments, memcpy
will compile usually into a CPU instruction which copies a whole memory area as fast as possible.
You can easily test the difference using an example program:
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
const int blockSize = 4096;
void copyA(char *a, char *b)
{
memcpy(b, a, blockSize);
}
void copyB(char *a, char *b)
{
for (int i = 0; i < blockSize; ++i) {
b[i] = a[i];
}
}
int main()
{
char *a, *b;
a = (char*)malloc(blockSize);
b = (char*)malloc(blockSize);
memset(a, 0, blockSize);
memset(b, 1, blockSize);
for (int i = 0; i < 80000000; ++i) {
copyA(a, b);
copyB(a, b);
}
return 0;
}
If you profile this program you get more or less this result:
In simple cases the compiler optimiser detects the simple attempt to "move memory" and automatically replaces the for
loop with a simple "memory move" instruction.
Compile this example, and check the disassembly generated by the C++ compiler to see the difference.
Add a printf("%s%s",a,b);
to make sure the compiler doesn't remove everything for optimisation.