Efficient Coding Practices

Francis H. Yu francis at proxftl.UUCP
Mon Oct 3 23:58:00 AEST 1988


In article <34112 at XAIT.XEROX.COM> g-rh at XAIT.Xerox.COM (Richard Harter) writes:
!
!	Let me give a simple example.  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.  Instead of using indices (presumably less
!efficent) we will use pointers.  Since we don't want to destroy dst
!and src as pointers we need temporaries.  Thus we might write
!
!	tmp1 = dst;
!	tmp2 = src;
!	for (i=0;i<n;i++) *tmp1++ = *tmp2++;
!
The better code is 
	tmp1 = dst;
	tmp2 = src;
	tmp3 = dst + n;
	while (tmp1 != tmp3) {
		*tmp1++ = *tmp2++;
	}



More information about the Comp.lang.c mailing list