A study in code optimization in C

Doug McDonald mcdonald at aries.scs.uiuc.edu
Fri Jul 27 00:41:34 AEST 1990


In article <133 at smds.UUCP> rh at smds.UUCP (Richard Harter) writes:
>
>The macro shown below is an optimized memory to memory copy macro.
>It is probably faster than memcopy on your machine -- I have checked
>it on several machines and have always found it to be faster.
                                 !!!!!!
>
Oh My!.

Time on my computer, in seconds, for 1000 copies of a 20 kilobyte array:

                          His code                   library memcpy            
Compiler 1:
               (chars)     12.6                            2.7
               (ints)       6.9                            2.7


Compiler 2:
               (chars)     23.6                            1.3
               (ints)       6.9                            1.3

Results were the same for library memmove, aligned or unaligned.

Sure looks like his code loses. As I would expect, of course:  The
library can do the whole 20 kilobytes in one instruction, assuming
aligned operands. Unaligned, it takes a few more.


When people post things like this, perhaps they could test them, at least
on the most common computers. My computer is an example of the next to
the top of the line of the most common processor line(*) the world has ever
known. At least, folks, test on one of these!!

Doug McDonald














(*) It is a 20 MHz Dell 310, 80386. The compilers were Microsoft C and
Microway NDPC.



More information about the Comp.lang.c mailing list