Xref: news2.mv.net comp.os.msdos.djgpp:5225 From: richard DOT young AT crc DOT doc DOT ca (Richard Young) Newsgroups: comp.os.msdos.djgpp Subject: Re: Speed optimization: memcpy() or for loop ?? ** Some actual test results ** Date: 19 Jun 1996 19:25:25 GMT Organization: Communications Research Centre Lines: 72 Message-ID: <4q9k75$bgg@crc-news.doc.ca> References: <4pmlrp$p7u AT crc-news DOT doc DOT ca> <31C2577D DOT 22FD99BD AT alcyone DOT com> <4q679q$4op AT snlsu1> NNTP-Posting-Host: yaker.vpcs.doc.ca Mime-Version: 1.0 Content-Type: Text/Plain; charset=US-ASCII To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp > 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 #include #include #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