Skip to content
Prev 36535 / 63424 Next

suggestion how to use memcpy in duplicate.c

Hi Matthew,
Matthew Dowle wrote:
On my system (DELL LATITUDE laptop with 64-bit 9.04 Ubuntu):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void *memcpy2(char *dest, const char *src, size_t n)
{
         int i;

         for (i = 0; i < n; i++) *(dest++) = *(src++);
         return dest;
}

int main()
{
         int n, kmax, k;
         char *x, *y;

         n = 25000000;
	kmax = 100;
         x = (char *) malloc(n);
         y = (char *) malloc(n);
         for (k = 0; k < kmax; k++)
                 //memcpy2(y, x, n);
                 memcpy(y, x, n);
         return 0;
}

Benchmarks:

n = 25000000, kmax = 100, memcpy2:

   real	0m8.123s
   user	0m8.077s
   sys	0m0.040s

n = 25000000, k = 100, memcpy:

   real	0m1.076s
   user	0m1.004s
   sys	0m0.060s

n = 25000, kmax = 100000, memcpy2:

   real	0m8.033s
   user	0m8.005s
   sys	0m0.012s

n = 25000, kmax = 100000, memcpy:

   real	0m0.353s
   user	0m0.352s
   sys	0m0.000s

n = 25, kmax = 100000000, memcpy2:

   real	0m8.351s
   user	0m8.313s
   sys	0m0.008s

n = 25, kmax = 100000000, memcpy:

   real	0m0.628s
   user	0m0.624s
   sys	0m0.004s

So depending on the size of the memory area to copy, GNU memcpy() is
between 7.5x and 22x faster than using a for() loop. You can reasonably
expect that the authors of memcpy() have done their best to optimize
the code for most platforms they support, for big and small memory
areas, and that if there was a need to branch based on the size of the
area, that's already done *inside* memcpy() (I'm just speculating here,
I didn't look at memcpy's source code).

Cheers,
H.