Date: Tue, 13 May 1997 09:42:21 -0400 From: Bryan Murphy Subject: Re: Any tips on optimizing C code? To: jon cc: djgpp AT delorie DOT com In-Reply-To: <33775c59.19219875@news.cis.yale.edu> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk > I'm interested in understanding what can be done to speed up straight > C code. In the specific thing I am writing, I've already done the > obvious things, like switched most calcs from FP to integer, using bit > shifting wherever possible for multiplying and dividing, etc. But is > there a complied source of information on just > what-is-faster-than-what? Like, does running a "for" loop by > decrementing rather than incrementing actually save a cycle? or does a > "case" command actually beat a series of "if"s? Do global variable > speed things up? I figure there must be something out there that has > the low-down on just this sort of nitty-gritty info. > > DJGPP is my compiler of choice, if that makes a difference. Have you profiled your code with gprof yet? That will give you the biggest insights. Optimizing is too big a topic and too dependant on your own coding style. If you run gprof, it'll let you know where the slow parts of your program are. Knowing what it is you have to optimize is the first most important step, then just look for short cuts or other ways around things. Here are a few tricks I've picked up along the way that really help: (1) Unroll For Loops (though I think DJGPP might already do this). (2) Use look up tables (3) Multiply by 1/numbers in stead of dividing in critical areas etc. etc. Just make sure you know what it is that needs optimization. There is no point in optimizing something that takes up only 2% of the cpu's power, when you are skipping something that takes up 90%.