[oclug] programming style vs performance
Greg
sphex at sympatico.ca
Tue Sep 20 18:40:09 EDT 2005
The fortran compiler, which I used back in the Pleistocene, would have
compiled both to pretty much identical code. In normal mode, it would
insert anonymous variables into your second. In optimizing mode, it
would have eliminated all stored variables from your first (assuming
sufficient registers). If the functions were known to the compiler, it
might have copied them inline. In debugging mode, it would even store
intermediate results of calculations, and do the invariant operations
for argThree at run time.
About that time, we were learning that, except in very extreme cases,
pushing computational resources, one efficient bug generally costs more
than all the inefficient CPU cycles a clever programmer might save in
his entire career. Also, most programmers are not clever.
On the other hand, the scientists and mathematicians with whom I worked
would write better and more accurate code in the style of your second
example. They would use minimal, almost cryptic, names... they were
simply copying equations out of their notebooks, in forms they were used
to. If you copy a formula out of a book, copy it exactly as possible
and comment the differences.
On the other other hand, unless there are really good reasons (reasons
that other people find really good too), one must always follow the
existing or official style. Even when you are right, if coworkers,
however wrong, struggle to read your code, you are wrong.
One good reason, which Adrian mentioned the other day, and which applies
to any kind of literature : if your sentences are muddled and confused,
then they certainly express muddled and confused thoughts, and probably
your muddled and confused thoughts along with your code are wrong.
Greg
Olaf Baumann wrote:
> Consider the following two snippets of functionally equivalent fictional
> code
>
> 1)
> int doSomething() {
> int argOne = getArgOne();
> int argTwo = createSomethingForTheSecondArgument();
> int argThree = 3 * 4 * SOME_CONSTANT;
> long result = doSomethingElse( argOne, argTwo, argThree);
> return result;
> }
>
> 2)
> int doSomething() {
> return result = doSomethingElse(getArgOne(),
> createSomethingForTheSecondArgument(), 3 * 4 * SOME_CONSTANT);
> }
>
>
> Which would you prefer?
>
> When I can, I try to write my code for the next programmer that has to
> understand/modify it. Personally, I would much rather work on code like
> listing 1. If needed, debugging code like listing 2 gets difficult as
> there is no line or local variables whose state can be viewed. Also,
> adding application logging to listing 2 would be impossible or worse, if
> the methods called had side effects.
>
> Is it possible that a compiler would produce faster code from listing 2
> (having fewer /named/ local variables on the stack)? Or would any
> compiler worth its salt optimize both to the same result. Not that I'm
> one to fall into the trap of premature optimization, but I would like to
> be aware of the possibilities.
>
> I've tried to keep the code mostly language-independent as answers may
> be language-specific and the differences between languages would be
> interesting to see. For example, I would guess that if this code was
> interpreted python (without any compilation stage), it would be slower
> if for no other reason, there are more characters in the source to scan.
>
More information about the OCLUG
mailing list