Efficient coding considered harmful?

Sean Fagan seanf at sco.COM
Thu Nov 3 11:21:14 AEST 1988


In article <273 at tijc02.UUCP> pjs269 at tijc02.UUCP (Paul Schmidt        ) writes:
>
>    After working for a year to optimize a DBMS I
>have some comments on writing efficient code.
>    Take, for example, my favorite:
>some_function(a_variable)
>short a_variable;
>    The coder (who was inexperienced in C) wanted
>to optimize the space needed to save the parameter
>passed to the function.  This actually may add to
>the memory and time to do conversion between short
>and int.

If I were to look at that, I would say that the coder used a short because
he/she wanted to document the fact that the value was not expected to
overflow a short. (is anybody following me?  if so, could you explain it to
me? 8-)  Here's another shot:)  I.e., "I don't expect the value to be larger
than 32k-1 [on most 32-bit machines, for example]."  It's what I would do.

In the rest of the article, he only says one thing I really agree with:

>    But there is still a need for optimization.
>This should be done after the code is written and
>working.  Why?  Because the amount of time spent
>in each code segment varies widely.

Also, Paul advocates the use of prof.  I agree (if anyones interested,
there's a story I have about how profiling enabled someone to cut the
execution time of a program from 9+ CPU hours on a Cyber [a *very* fast
number cruncher] to about 2-5 minutes [on the same machine, of course]).

Most programmers (in C) that I know tend to do some "microoptimizations" as
they write (i.e., "a&0xf" instead of "a%16" type of thing).  Afterwards, I
(and they) tend to look at the program, try to understand what I wrote, and
see how I can improve it (which isn't always easy).  For me, this process is
modified by how the compiler optimizes, and what the machine architecture
I'm on dictates (I admit that I don't necessarily write portable, optimal
code.  Tough. 8-)).

Basicly, what I'm trying to say is that not all microoptimization is bad.

>    Paul Schmidt
>    mcnc!rti!tijc02!pjs269

-- 
Sean Eric Fagan  | "Engineering without management is *ART*"
seanf at sco.UUCP   |     Jeff Johnson (jeffj at sco)
(408) 458-1422   | Any opinions expressed are my own, not my employers'.



More information about the Comp.lang.c mailing list