[comp.sys.mac] "Efficient" C programming - was Re: New Mac Rumours

pratt@boulder.Colorado.EDU (Jonathan Pratt) (02/28/89)

In article <15682@versatc.UUCP> peter@versatc.UUCP (Peter Tapscott ) writes:
...
>It is also possible to write the c-code to produce optimum execution speed.
>For example, the following four lines of code execute 4X faster than
>the two lines using a FOR loop:
>
>Fast While Loop:
>        i = char_count;
>        while(i--)
>            destn_buffer[i] = *(source_buffer + i);
>        *destn_buffer = *source_buffer;  /* get the zeroth element */.
>
>Slow For Loop:
>		for (i = 0; i <= char_count; i++)
>              destn_buffer[i] = *(source_buffer + i);
>
I decided to take issue with this particular generalization, so I coded
these two code fragments and examined the resulting assembly language
from two compilers (LSC 3 and MPW).  In each case the supposedly slow
for loop generated shorter and slightly faster code.  My point is that
a great deal depends on the compilers involved.  It's pushing things
too far to make a "4x faster" generalization for such similar code.

>This is because the while loop makes use of the DBNE (decrement and

In my trial cases neither compiler was smart enough to use DBxx
instructions.  I'd say that beyond the obvious things, like using the
best algorithm and using pointers/registers instead of random indexing,
your best bet for writing efficient HLL code is to be well acquainted
with your compiler.  Opinions, for/against?  Note followups to *.programmer.

Jonathan


/* Jonathan Pratt          Internet: pratt@boulder.colorado.edu     *
 * Campus Box 525              uucp: ..!{ncar|nbires}!boulder!pratt *
 * University of Colorado                                           *
 * Boulder, CO 80309          Phone: (303) 492-4293                 */