Link to home
Start Free TrialLog in
Avatar of mych
mych

asked on

Implementing timing control in loop

I would like to iterate a loop, say once every few milliseconds. I tried using Sleep to do it. However, upon printing out the time using GetTickCount(), I notice that the timing interval is always rounded up to the next 10 milliseconds.

For example, if the code is something like the following.

for(i = 1; i <= 100; i++)
{
tickcount = GetTickCount();
printf("\n%lu",tickcount);
Sleep(25);
}

The printed result would be something like:
xxx12
xxx42
xxx72
and so on. That is, whether I put Sleep(21), Sleep(22) or whatever, the interval would never be accurate to the millisecond but would be rounded up to the next 10 milliseconds.

Is there any way to implement some timing control so that it is more accurate?


Avatar of ufolk123
ufolk123

Windows is not supposed to give you a real time response.As it is not a RTOS.There can be lot of dependencies on system load and priorties etc which can affect the exact timings you may be expecting.


Hi,
  Your question are too general as the timing (ie accuracy etc) depends on the systems used. What platform are using WINNT? WIN9x? or someother Oses? How about hardware ..? 386?486?Pentium.? or some other processors(because different hardware have different realtime clock).

Anyway, generally.. GetTickCount() will give u a value the way your system define 1 tick. Ie.. if your one tick is 10 milliseconds then your realtime clock is updated every 10 milliseconds (in every timer interrupt) and to increase the accuracy u would have to reduce it(but you have to pay penalty where timer interrupts occur more frequent and might slow down other processes).

Other things is that your method is *not* suitable for measuring accurate time granularity since, If you are in multitasking env. then your timing loop may get *preempted* by a higher prority tasks or interrupt. And the task may take cpu time longer than you expected. Also, in many system(ie WINNT, WIN9x) the interrupt lantency is resonably *high*.

The better way is to use a high resolution timer routines supplied by your system(if any :)) and used the elapsed time to increment your time. In this way, your timer will be *least* affected by problem mention above.

Hope this helps.

Best Regards,
Nisa.      
Avatar of mych

ASKER

Hi Nina,
Can you give some examples of high resolution timer routines supplied by the system? By the way, I'm using WinNT on Pentium III.
You can use the QueryPerformanceCounter() and QueryPerformanceFrequency() .Usage may be little bit differnt from that of SetTimer().
ASKER CERTIFIED SOLUTION
Avatar of nisa
nisa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm not sure if this will be of any help, but if you are using gcc you can include time.h, and use the rawclock() functions.  It operates on 91.02 (rounded to 91) ticks per second.  Just a thought....

the_bikeman