Why does memcpy perform slower than memmove on my system?
From reading other SO questions such as this or this Gives the impression that memcpy should work faster than memmove, and intuitively, this should be so. After all, there are less checks that memcpy has and the man pages also match what they say.
However, when measuring the time spent inside of each function, memmove beats memcpy! What more, it seems to beat memset too, when memset seems like it could benefit from optimizations that memcpy or memmove can't. Why would this be so?
Results (one of many) on my computer:
[INFO] (ex23.c:151 func: main) Normal copy: 109092
[INFO] (ex23.c:198 func: main) memcpy: 66070
[INFO] (ex23.c:209 func: main) memmove: 53149
[INFO] (ex23.c:219 func: main) memset: 52451
Code used to give this result:
#include <stdio.h>
#include <string.h>
#include "dbg.h" // debugging macros
#include <time.h>
int main(int argc, char *argv[])
{
char from[10000] = {'a'};
char to[10000] = {'c'};
int rc = 0;
struct timespec before;
memset(from, 'x', 10000);
memset(to, 'y', 10000);
clock_gettime(CLOCK_REALTIME, &before);
// naive assignment using a for loop
normal_copy(from, to, 10000);
struct timespec after;
clock_gettime(CLOCK_REALTIME, &after);
log_info("Normal copy: %ld", (after.tv_nsec - before.tv_nsec));
memset(to, 'y', 10000);
clock_gettime(CLOCK_REALTIME, &before);
memcpy(to, from, 10000);
clock_gettime(CLOCK_REALTIME, &after);
log_info("memcpy: %ld", (after.tv_nsec - before.tv_nsec));
memset(to, 'y', 10000);
clock_gettime(CLOCK_REALTIME, &before);
memmove(to, from, 10000);
clock_gettime(CLOCK_REALTIME, &after);
log_info("memmove: %ld", (after.tv_nsec - before.tv_nsec));
memset(to, 'y', 10000);
clock_gettime(CLOCK_REALTIME, &before);
memset(to, 'x', 10000);
clock_gettime(CLOCK_REALTIME, &after);
log_info("memset: %ld", (after.tv_nsec - before.tv_nsec));
return 0;
}