Mail Archives: djgpp/1996/06/20/21:24:30
> Richard Young wrote:
> > A question for the optimization experts:
> > For moving data, is it faster to use
> >
> > a) memcpy(x,y,n*sizeof(x[0]))
> > or
> > b) for (i = 0; i < n; i++) x[i] = y[i];
> >
> > or are they basically the same speed.
Well, I wrote a test program to test several data move options:
1) memcpy()
2) an index based for loop
3) a ptr based for loop
The results are: NO DIFFERENCE.
On my 120 MHz DEC Pentium executing a data move of 100000 floats 2000 times
took roughly 45 seconds for all options.
I'm a little surprised that memcpy() wasn't faster. Am I getting an inlined
"C" version instead of an assembly coded library function.
My test program below was compiled with
gcc -O3 -m486 -otestmemc testmemc.cpp
//--------------------------------------------------------
// testmemc.cpp
#include <time.h>
#include <string.h>
#include <stdio.h>
#define I 100000
#define N 2000
float x[I];
float y[I];
void main()
{
time_t t1 = time(NULL);
for (int n = 0; n < N; n++)
memcpy(&y[0],&x[0],I*sizeof(float));
time_t t2 = time(NULL);
printf("memcpy time = %f\n",difftime(t2,t1));
time_t t3 = time(NULL);
for (int n = 0; n < N; n++)
for (int i = 0; i < I; i++)
y[i] = x[i];
time_t t4 = time(NULL);
printf("Index loop time = %f\n",difftime(t4,t3));
time_t t5 = time(NULL);
for (int n = 0; n < N; n++)
{
float* xp = &x[0];
float* yp = &y[0];
for (;xp <= &x[I];)
*yp++ = *xp++;
}
time_t t6 = time(NULL);
printf("Ptr loop time = %f\n",difftime(t6,t5));
}
--------------------------------------------------------
Richard Young
CRC, Ottawa, Canada
- Raw text -