Efficient Coding Practices

Chris Torek chris at mimsy.UUCP
Tue Oct 4 05:46:58 AEST 1988


>In article <34112 at XAIT.XEROX.COM> g-rh at XAIT.Xerox.COM (Richard Harter) writes:
>>Ignoring library routines, inline routines, etc, suppose that we want
>>to copy n bytes from one place to another, say array src to array dst.
>>We might write
>>	for(i=0;i<n;i++) dst[i]=src[i];

>>Let's hand optimize this.

[This was part of an argument *against*, but let that stand:]

>>	tmp1 = dst;
>>	tmp2 = src;
>>	for (i=0;i<n;i++) *tmp1++ = *tmp2++;

In article <846 at proxftl.UUCP> francis at proxftl.UUCP (Francis H. Yu) writes:
>The better code is 
>	tmp1 = dst;
>	tmp2 = src;
>	tmp3 = dst + n;
>	while (tmp1 != tmp3) {
>		*tmp1++ = *tmp2++;
>	}

Better for whom?

On a 68010, the following is *much* better:

	register short n1;
	if ((n1 = n - 1) >= 0)
		do *tmp1++ = *tmp2++; while (--n1 != -1);

because it can compile into a `dbra' loop and take advantage of the
68010 loop mode.  But this is much less efficient on a VAX than the
`movc3' instruction that the compiler might generate for the original
loop.  But the second way is better for the Foobar CPU, which has a
`count-up' loop mode; but the third is better for the BazWoRKS chip.

This is micro-efficiency at its finest: you cannot characterise it
outside of its environment.  Which loop is `best' is heavily machine
dependent.  If that loop takes much time, go ahead and optimise it,
but if not, you might as well not bother, since everyone else will
just have to re-optimise it differently anyway.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list