May too many register variables hurt? (was Re: Novice question.)

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Fri Nov 30 19:33:30 AEST 1990


In article <RJC.90Nov29012724 at brodie.uk.ac.ed.cstr> rjc at uk.ac.ed.cstr (Richard Caley) writes:
  [     void tweakit(register struct foobar *lastone) ]
  [ vs. void tweakit(struct foobar *lastone) ]
> I find the second _so_ much more readable

Oh, fgs. I find both of them equally unreadable.

With pre-ANSI syntax, it's trivial to see the variable right near the
end of a line, and to work backwards through its declaration to see how
it's used. (How else can you read a declaration?) Adding ``register'' or
another keyword to the beginning of a line hardly impairs this skill.

> that adding in the register
> for, say, a 10% speedup in a program which runs in 10 seconds is not
> worth it unless you are _very_ sure that that second is critical and
> the code is more or less totally stable.

Well, fine. In every program's life there comes a time when it must
inspect itself, and say, ``Yea, verily, my registers are hardly used,
and it is time for me to give up the slowness of immaturity, and take on
some register declarations, so that I can run at a respectable speed,
until death do us part.'' Or something like that.

I'm just tidying up a program whose main code has 19 register
declarations. I knew as I added each of those variables that it would be
used a few times in the inner loop, and the nature of the problem meant
that none of the variables would be used much more often than the
others.

Should I take away any of those declarations? Don't be silly.

Did I have to wait for the code to be stable to do this optimization?
Not at all.

What happens when I remove the declarations? With optimization, nothing.
Without optimization, #define register null makes it run 40-80% slower.

You have to be crazy to say that programmers should take such a penalty
during development, for no more reason than ``register looks ugly'' or
``it *could* slow down your code.''

> If the time _is_ critical then it is not enough to stick in a few
> register variables anyway, it is time to wheel out the profiler, stare
> at the assembler output and work out whether floating point arithmetic
> or jumps are more time critical on your machine.

True. Every optimization helps.

---Dan



More information about the Comp.lang.c mailing list