memcpy versus assignment

Tim Olson tim at nucleus.amd.com
Sat Dec 30 03:02:46 AEST 1989


In article <1657 at uwm.edu> chad at 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 at amd.com)



More information about the Comp.lang.c mailing list