帮助使用 memcpy C 在缓冲区之间进行复制
我需要将一个缓冲区的内容以 n 字节块(n 可能会有所不同)的形式复制到另一个缓冲区,多次以检查缓存性能。
我使用 memcpy,但恐怕我没有得到成功的结果。块大小从几千字节到几兆字节不等。我必须保留要使用的最大块(long double)。
我对副本有点迷失。我只是想知道是否有人遇到过这个问题,并且可以帮助我提供一些提示或伪代码。
我编辑主题以包含一些代码:
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, ¶metros);
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 \t Tiempo 1: %.2f \t Fallos Metodo 1:%d \t Tiempo 2: %.2f \t Fallos Metodo 2:%d \t Multiplica: %f \t Rendimiento: %.2f\n", i, total, fallos1, total2, fallos2, iteraciones, rendimiento);
// fprintf (fp, "%d \t %.3f %.3f %.3f\n", i, total, total2,rendimiento);
fclose(fp);
printf("Se ha creado el archivo de datos: part1.dat\n");
return(0);
}
想法是从 buffer1 复制到 buffer2,在 datos 块“p”次中,在这种情况下应该完成 256000 次(i=1,datos=1024)。
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, ¶metros);
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 \t Tiempo 1: %.2f \t Fallos Metodo 1:%d \t Tiempo 2: %.2f \t Fallos Metodo 2:%d \t Multiplica: %f \t Rendimiento: %.2f\n", i, total, fallos1, total2, fallos2, iteraciones, rendimiento);
// fprintf (fp, "%d \t %.3f %.3f %.3f\n", i, total, total2,rendimiento);
fclose(fp);
printf("Se ha creado el archivo de datos: part1.dat\n");
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码有问题 - 内部 for() 循环不会执行。您将 p 初始化为零
,然后循环条件比较 p > > i 其中 i 的范围从 1 到 64。
所以 memcpy() 实际上从未被调用过...
您还说过:
memcpy() 将目标数组作为第一个参数,将源数组作为第二个参数。你的参数是倒退的。
A problem with the code - the inner for() loop won't execute. You are initializing p to zero
and then the loop condition compares p > i where i ranges from 1 to 64.
So memcpy() is never actually called...
You also said:
memcpy() takes the destination array as the first argument and the source as second. Your parameters are backwards.
您可能不应该为此使用
memcpy()
,因为您不知道它的内部行为。例如,它可能会乱序地触摸内存,这可能会使您的测量结果变得奇怪。当然,它会被认为是做“最好的事情”,但你的任务似乎暗示你不在乎。 :)因此,只需使用直接复制循环,也许首先舍入非对齐访问(这是 memcpy() 很可能已经做的另一件事)。
此外,您无法使用非整数类型(例如
double
)来测量内存块大小。您应该使用size_t
。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 usesize_t
.