/* memcpy cost measurement. * * On one core of my 2.4GHz Pentium N3700: * memcpying a 1-byte file costs about 18 ns, about 60 megabytes/second. * memcpying an 18-byte file costs about 17 ns, about 1 gigabyte/second. * memcpying a 40-byte file costs about 21 ns, about 2 gigabytes/second. * memcpying a 9KB file costs about 3 microseconds, about 3 gigabytes/second. * memcpying a 263,432,738-byte file costs about 140 milliseconds, about 1.9 gigabytes/second. * * Running concurrently on two cores doesn’t slow it down * substantially; on four cores it slows down by half for the large * file, but naturally not for a small one. I am surprised that * there's apparently never a main memory bottleneck until you’re * running on more than two cores. */ #include #include #include #include #include #include #include int main(int argc, char **argv) { int f = open(argv[1], O_RDONLY); int n = atoi(argv[2]); if (f < 0) { perror(argv[1]); return 1; } int size = lseek(f, 0, SEEK_END); char *m = mmap(0, size, PROT_READ, MAP_SHARED, f, 0); if (MAP_FAILED == m) { perror("mmap"); return 1; } char *p = malloc(size+1); if (!p) { perror("malloc"); return 1; } for (int i = 0; i != n; i++) { memcpy(p, m, size); } return 0; }