Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!sun-barr!decwrl!amdcad!nucleus!tim From: tim@nucleus.amd.com (Tim Olson) Newsgroups: comp.lang.c Subject: Re: memcpy versus assignment Message-ID: <28549@amdcad.AMD.COM> Date: 29 Dec 89 16:02:46 GMT References: <1657@uwm.edu> Sender: news@amdcad.AMD.COM Reply-To: tim@amd.com (Tim Olson) Distribution: na Organization: Advanced Micro Devices, Inc., Austin, Texas Lines: 37 Summary: Expires: Sender: Followup-To: In article <1657@uwm.edu> chad@csd4.csd.uwm.edu (D. Chadwick Gibbons) writes: | In several books I've seen that assignment of structures is usually | more efficient than using memcpy(), at leant on most modern | processors. I did a few experiments to see if this is true...using | the following short program, I attempted to extract the machine | code produced on different machines. [ code examples deleted ] | However, this is only have the question. Does the assignment win | over memcpy? On the 8086, the following code is produced: | In _general_ what is the rule for the assignment of two large | structures? memcpy vs. assignment? Which is generally better? Assignment should *always* be better, or at least equal to a call to memcpy, in terms of performance. The compiler knows (at compile time) all of the sizes and alignments of the structures being copied, so it can choose the most efficient assignment method for a given processor. For example, if the structures are aligned and padded to 4-byte boundaries on a 32-bit processor, then 32-bit load/store instructions can be used to copy the structure 4 bytes at a time. A general-purpose runtime routine such as memcpy must perform the copy a byte at a time, or perform runtime checks on the size and alignments of the memory areas being copied. If the compiler recognizes and inlines library routines, then a call to memcpy() may be as fast as structure assignment, but you are better off using the assignment, because: 1) it will be more efficient on many machine/compiler combinations 2) it expresses the programmer's intent more clearly -- Tim Olson Advanced Micro Devices (tim@amd.com)