Xref: news2.mv.net comp.os.msdos.djgpp:4941 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 ?? Date: 12 Jun 1996 19:56:41 GMT Organization: Communications Research Centre Lines: 54 Message-ID: <4pn7dp$2d2@crc-news.doc.ca> References: <4pmlrp$p7u AT crc-news DOT doc DOT ca> <4pmscu$nrt AT rs18 DOT hrz DOT th-darmstadt DOT de> 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 In article <4pmscu$nrt AT rs18 DOT hrz DOT th-darmstadt DOT de>, lehmann AT mathematik DOT th-darmstadt DOT de says... > >Richard Young (richard DOT young AT crc DOT doc DOT ca) 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. > >: With C++ is it better code practice to use b) over a)? > >(a) uses the function dj_movedata, which will use the repeat >instruction to copy 4 byte values, which should be pretty fast. > >(b) requires a lots of address calculations, unless the compiler is >very smart (I don't think so), but it can be sped up a bit at least >(assuming that x and y are of type foo): > >foo *px,*py; >int i; > >for(i=n,px=x,py=y; i ; i--) *py++=*px++; > >You still don't not get a repeat instruction, but at least you save >the index calculations. > > >bye, Alexander > >-- >Alexander Lehmann, | "On the Internet, >alex AT hal DOT rhein-main DOT de (plain, MIME, NeXT) | nobody knows >lehmann AT mathematik DOT th-darmstadt DOT de (plain) | you're a dog." >!!CHANGED!! With the powerful offset addressing modes of today's processors I find it hard to believe that for(i=n,px=x,py=y; i ; i--) *py++=*px++; is faster than for (i = 0; i < n; i++) x[i] = y[i]; I would also expect that the code optimizers should be able to invoke repeat instructions for tight loops. Just how good or bad are the optimizers?