Efficient programming - a maxim

mcdonald at uxe.cso.uiuc.edu mcdonald at uxe.cso.uiuc.edu
Mon Oct 10 00:52:00 AEST 1988


>Some advice on efficient programming is:

>  Never do the same thing more than once

>That implies using temporary variables to hold the results of operations for
>later reuse.  


example: 
   x = (a+b)*(c+d)-(c-d)/(a+b);

versus
   t = a+b;

   x = t*(c+d)-(c-d)/t;


I have checked this sort of thing, and larger examples, on many compilers,
including vector machines, machines with poor optimizers, mediocre
optimizers, and stupendous optimizers (VAX C). With a stupendous optimizer
it of course doesn't matter one bit. With poor or mediocre optimizers,
sometimes one is better, sometimes the other. You just have to try it.
This is why hacks and tweaks have to come last, and are non-portable.
Just like your momma taught you.



More information about the Comp.lang.c mailing list