English 中文(简体)
Help with a copy between buffers using memcpy C
原标题:
  • 时间:2009-12-16 12:30:03
  •  标签:
  • c
  • memcpy

I need to copy the content of one buffer to another one in blocks of n bytes (n might vary), several times to check the cache performance.

I use memcpy, but I m afraid I m not getting successful results. The block size is variable from some kbytes to Mbytes. And I have to reserve the maximum block to use (long double).

I m a little lost in the copy. I m just wondering if somebody has faced this problem, and can help me with some tips or pseudo code.

I edit the topic to include some code:

int main (int argc, char *argv[])
{
    FILE *fp;
    fp= fopen("part1.dat", "w");
    struct timeval time, oldtime;
    float segundos, microsegundos, total;
    //        float rendimiento;    
    pid_t pid;
    struct sched_param parametros;
    int i, v,p;
    char buffer1[100024];
    char buffer2[100024];

    pid = getpid();
    parametros.sched_priority = sched_get_priority_max(SCHED_FIFO);
    sched_setscheduler(pid, SCHED_FIFO, &parametros);
    p=0;

    gettimeofday(&oldtime, NULL);

    for (i=1;i<65;i++)
    {
        size_t datos=i*1024;
        for (v=0; p>i;v++)
        {
             memcpy(buffer1, buffer2, datos);
             p=(MAX_SIZE/i*1024);

        }
    }

    gettimeofday(&time, NULL);
    segundos = (float) (time.tv_sec - oldtime.tv_sec);
    microsegundos = (float) (time.tv_usec - oldtime.tv_usec);
    total = (float) ((segundos * 1000000 + microsegundos));


    //            printf ("Dimension %d 	 Tiempo 1: %.2f 	 Fallos Metodo 1:%d 	 Tiempo 2: %.2f 	 Fallos Metodo 2:%d 	 Multiplica: %f 	 Rendimiento: %.2f
", i, total, fallos1, total2, fallos2, iteraciones, rendimiento);
    //              fprintf (fp, "%d 	 %.3f %.3f %.3f
", i, total, total2,rendimiento);


    fclose(fp);
    printf("Se ha creado el archivo de datos: part1.dat
");

    return(0);
}

The idea is to copy from buffer1 to buffer2, in datos blocks p times, in this case is supposed to be done 256000 times (i=1, datos=1024).

问题回答

A problem with the code - the inner for() loop won t execute. You are initializing p to zero

p = 0;

and then the loop condition compares p > i where i ranges from 1 to 64.

for (i=1;i<65;i++)
{
   size_t datos=i*1024;
   for (v=0; p>i;v++)
   {
      .....

So memcpy() is never actually called...

You also said:

The idea is to copy from buffer1 to buffer2, in datos blocks p times, in this case is supposed to be done 256000 times (i=1, datos=1024).

memcpy() takes the destination array as the first argument and the source as second. Your parameters are backwards.

You probably shouldn t use memcpy() for this, since you don t know how it acts internally. It might for instance touch memory out of order, which could perhaps make your measurements odd. It would of course be assumed to do "the best thing", but your task seems to imply that you don t care. :)

So just use a straight copying loop, maybe rounding out non-aligned accesses first (which is another thing memcpy() very probably already does).

Also, you can t measure memory block sizes using a non-integer type such as double. You should use size_t.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签