Just rambling about optimization...

Stephen Clamage steve at taumet.com
Fri May 10 01:13:11 AEST 1991


dsimon at eniac.seas.upenn.edu (Derron Simon) writes:

>One utility I was looking for about a year ago is an optimizer for C code
>that can parse and optimize the original source.  I don't know why this hasn't
>been attempted by any PD writers.  It is definately non-trivial, but I'm 
>surprised no one has ever attempted it.

I'm not surprised.  It is of very little use.  Gains in performance at
the source code level are best achieved by choosing better algorithms.
Reliable optimizations achievable by source code manipulation as you
describe are done anyway by quality compilers.  Many of the best
optimizations depend critically on how the compiler generates code,
and on the characteristics of the machine.  For example, an "obvious"
optimization is to jump around unnecessary code.  Yet on some pipelined
machines, the penalty for a jump is so high it is actually faster to
execute the useless instructions.  Another "obvious" optimization is
to convert certain kinds of multiplication into shifts and adds.  The
right way to do this depends critically on the machine -- some shifts on
some machines are slower than some multiplies.  The compiler can do this
best, and quality compilers do it.

Code which is fiddled to run faster with a given compiler/machine
combination may run much slower with another combination.  The result
of the fiddling is generally not very readable, and hard to maintain.
If you can't get a quality compiler and you have positively identified
a bottleneck in your code, that is the time to look for a way to improve
that stretch of code.  Keep the original straightforward code around
and document your fiddling, so that later maintainers can understand
what has happened and why.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list