Excessive optimization (was Re: what was VMS/OpenVMS written in?)

Eric Smith eric at brouhaha.com
Fri Dec 3 15:56:54 CST 2010


Charlie Carothers wrote:
>
> I'm afraid I must disagree.  What if, for my own nefarious purposes, I 
> need the N microseconds of delay achieved via the "spurious" for loop? 
> I know, code loop delays are generally a poor practice, but what does 
> one do if a delay is absolutely required before timer interrupts can 
> be enabled?  What if there is no hardware timer (e.g. 8254) that can 
> be read periodically to time the delay?  What if I am writing a quick 
> hardware test?
>
Disagree all you want, but the C language offers no guarantees about how 
long a loop (or anything else) takes to execute, so a compiler 
optimizing away an empty loop is perfectly valid.  You can try compiling 
with optimizations off, but there's not really any guarantee with that 
either.  If I had to have a delay of a known length, and had no timer 
available, I'd call an assembly language subroutine.

You could try a more complicated loop in C:

   int i, j;
   j = 0;
   for (i = 1; i <= 100; i++)
       j += i;

But a really clever compiler could still replace all of this with:

   int i, j;
   i = 101;
   j = 5050;

Eric




More information about the cctech mailing list