Path: utzoo!attcan!uunet!husc6!XAIT!g-rh From: g-rh@XAIT.XEROX.COM (Richard Harter) Newsgroups: comp.lang.c Subject: Efficient Coding Practices Message-ID: <34112@XAIT.XEROX.COM> Date: 1 Oct 88 15:55:21 GMT References: <8809191521.AA17824@ucbvax.Berkeley.EDU> <68995@sun.uucp> <23025@amdcad.AMD.COM> <607@ardent.UUCP> <836@proxftl.UUCP> Reply-To: g-rh@XAIT.Xerox.COM (Richard Harter) Organization: Xerox Corporation, Cambridge, Massachusetts Lines: 55 In article <836@proxftl.UUCP> bill@proxftl.UUCP (T. William Wells) writes: >At one time, I spent a lot of my spare time improving other >people's code. One of the things I discovered is that almost all >the code I was dealing with had better than 25% execution time >fat. What I mean is that when I merely changed all of the code >to follow my coding practices (which are not anything special, >just obvious things that every coder ought to know), it got that >much better; I am not talking about mere tweaks, nor am I talking >about optimizing particular sections of code. I am talking about >reading each line of code and making obvious changes (things like >using temporaries when needed, adding appropriate register >declarations, eliminating obviously unnecessary function calls, >etc.) I would say that we all know what Bill is talking about here, except that "we" all obviously don't. Basically this is using hand optimization as a default in coding style. One can take the view that a good optimizer will do many of these things, e.g. use of temporaries, moving static expressions outside loops, placing the appropriate variables in registers, etc. One can also take the view that the capabilities and realities of optimizing compilers are less than claimed. Hand optimizing is safer across a variety of environments. In my experience hand optimizing in original code is less efficient, in the sense of code written per unit time, then writing code initially in a "clear" style first, and then hand optimizing afterwards. In short, write it in the simplest way first, get it working, and then improve performance. ... Discussion of practical superiority of pointers deleted. 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