From: Engard Ferenc Newsgroups: comp.os.msdos.djgpp Subject: Re: timing with empty loops -- small green ufo-s? Date: Mon, 22 Feb 1999 16:21:34 +0100 Organization: IIF Lines: 53 Message-ID: NNTP-Posting-Host: pons.sote.hu Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII In-Reply-To: <8D53104ECD0CD211AF4000A0C9D60AE3579368@probe-2.acclaim-euro.net> To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On Mon, 22 Feb 1999, Shawn Hargreaves wrote: >Engard Ferenc writes: >> My program does it's timing with empty loop: >> >> void do_delay (unsigned long i) >> { while(i--) ; } > >I don't think that is a very good way to do a timing delay. This is >incredibly prone to compiler optimisation, and will be affected by >things like the code alignment, processor cache, and if you inline >it within a larger function, the surrounding code will change how gcc >deals with it. Your point is absolutely good; I have arrived at the same idea. My solution was that I made it static in memory (so far I used it as inline function), and now it works quite well. > >I wouldn't use a delay loop for anything at all critical, which it >sounds like your situation is. If you really insist upon using this >method, though, at the very least you should: > >- write it in asm. >- put it a separate .s file, not inlined within C a source. >- use .align directives to make sure the routine cannot move around. >- only have a single copy, used for both calibration and actual delays. Now it has only a single copy in RAM, and as far as I know, at -O1 optimization level the gcc won't inline it if I don't ask it explicitely. I didn't disabled the interrupts, assuming it doesn't make too big deviation. Anyway, it is a good idea as I noticed that the control is going nuts if I press a key meanwhile the actual timings... :)) So, it looks I don't need to write it in asm. >If you do that, it will probably be pretty reliable, as long as you >can guarantee no external interference (eg. interrupts are disabled). >If this needs to be really accurate, though, it would be much better >to use a proper timing chip. The pentium has some hardware registers >specifically for this purpose, but I'm afraid I don't have any >references to hand on exactly how you would access them... It would be the best, just I have never programmed in asm... :-( Thanks for the tips: Circum __ @ / \ _ _ Engard Ferenc l | ( \ / | | (\/) mailto:s-fery AT kkt DOT sote DOT hu \__/ | | \_ \_/ I I http://pons.sote.hu/~s-fery