Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!henry From: henry@utzoo.UUCP (Henry Spencer) Newsgroups: comp.lang.c Subject: Re: goto's in C: an opinion... Message-ID: <8361@utzoo.UUCP> Date: Sun, 2-Aug-87 20:29:20 EDT Article-I.D.: utzoo.8361 Posted: Sun Aug 2 20:29:20 1987 Date-Received: Sun, 2-Aug-87 20:29:20 EDT References: <3289@bigburd.PRC.Unisys.COM>, <28700017@ccvaxa> Organization: U of Toronto Zoology Lines: 32 > When you've completed an application nicely in C, > using functions everywhere, and it's still too slow, > which would you rather do?: > > (1) Rewrite parts of it in assembler, > > or (2) Use gotos? Neither. First, you *profile* it, to find out where the time is really going; human guessing (even if dignified by names like "intuition" or "professional judgement") is usually wrong. Then, you revise the code in the hot spots to use better algorithms. If you are really greasing the code hard, it may be necessary to merge previously-separate functions into their callers, to reduce call overhead. The big wins come from reorganization to avoid work entirely, not from twiddling to do the work a little bit faster. For example, C News is now 1.5 orders of magnitude faster than B News, and will probably be still faster when Geoff and I release it, even though it contains no gotos and no assembler. We both dislike gotos, and avoid assembler on the grounds that it's unnecessary and a maintenance nightmare for portable code. (There ARE occasions when small assembler functions can be a win, if you are trying to perform operations that C isn't too good at, but often a radically new algorithm -- in C -- is still faster.) Geoff does a lot of profiling and code analysis and rewriting (I do some too, but his parts of the code are the hardest and most speed-critical ones), and we both think a lot about ways to avoid overhead. For the gory details, see our paper in the Winter 1987 Usenix proceedings. We may do a followup paper someday about all the things we've thought of since. -- Support sustained spaceflight: fight | Henry Spencer @ U of Toronto Zoology the soi-disant "Planetary Society"! | {allegra,ihnp4,decvax,utai}!utzoo!henry