Xref: news2.mv.net comp.os.msdos.djgpp:5129 From: devitto AT senna DOT ferndown DOT ate DOT slb DOT com (Dom De Vitto) Newsgroups: comp.os.msdos.djgpp Subject: Re: Speed optimization: memcpy() or for loop ?? Date: 18 Jun 1996 12:26:34 GMT Organization: Schlumber Technologies (Board Systems) Lines: 51 Message-ID: <4q679q$4op@snlsu1> References: <4pmlrp$p7u AT crc-news DOT doc DOT ca> <31C2577D DOT 22FD99BD AT alcyone DOT com> NNTP-Posting-Host: senna.ferndown.ate.slb.com To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp On Fri, 14 Jun 1996 23:26:05 -0700, Erik Max Francis (max AT alcyone DOT com) wrote: : 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. If typeof(x) is a POD type (plain old data) then is it likely that the system-supplied version is (hand) written in assembler, and so could be much faster - but no slower (ignoring a single function call overhead). : Use the standard library function. Good advice, for POD types. : The whole idea behind having such a function is that it is supposed to be a : good implementation of the operation you're interested in. And, furthermore, : it knows which platform it's running under, so to speak, and so it can do the : right thing. Except that memcpy() is a C function, nothing to do with C++ - it doesn't know exactly how to copy objects is - so can't "do the right thing". If the array is non-POD, using memcpy is just plain wrong, and the loop will be right - but slow, maybe much slower, because it's actually doing more. If the array is a POD type, then you really should use memcpy as it's a standard C function. Incedentally, memcpy(x,y,sizeof(y)) will be faster if you know that 'n' is constant. : > With C++ is it better code practice to use b) over a)? : Same thing. Different thing. Dom -- _____________________________________________________________________________ Dom De Vitto devitto AT ferndown DOT ate DOT slb DOT com Schlumberger Automatic Test Equipment fox AT bcs DOT org DOT uk Board Systems Desk/voicemail: +44(0) 1202 850951 Wimborne, Dorset, Site reception: +44(0) 1202 850850 England, BH21 7PP Fax: +44(0) 1202 850988 _____________________________________________________________________________