[Klug-general] Obscure C at saturday's meeting

Mike Evans mike at tandem.f9.co.uk
Wed Dec 1 14:13:05 UTC 2010


>
> OK I understand that in the scheduler you want good performance and
> compact code to get the most out of it, but some things were never meant
> to be written.
>
This may have been true once - but hasn't been so for a long time.  This 
is because an optimising compiler will produce the same or better 
machine code from simply written, well structured and legible program 
code.

I remember (about 18 years ago) in a quick panic wanting to slow 
something down and just added:

for (i=0, i<1000, i++) /* do nothing */;

Yes - I know I should have used sleep(), but never mind.  This didn't 
make the code slow enough, so I changed it to:

for (i=0, i<1000000, i++) /* do nothing */;

and it didn't take any longer.  When I stopped the compiler at the 
assembly stage and looked at the machine code what the compiler had 
actually done was the equivalent of:

i=1000000;

Served me right really.  Valuable lesson though: write the code as 
clearly as possible and let the compiler optimise.  It will probably do 
a better job than me, and will introduce fewer bugs in the process.




More information about the Kent mailing list