Mail Archives: djgpp/1996/06/18/11:07:00
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
_____________________________________________________________________________
- Raw text -