Re: Problem with QueryPerformanceCounter

From: Corey Murtagh (emonk_at_slingshot.no.uce)
Date: 01/08/04


Date: Thu, 08 Jan 2004 17:06:13 +1300

Atri Mandal wrote:

> Hi,
> I'm using the QueryPerformanceCounter() method to calculate the elapsed
> time in a block of code.
> It actually gave me results that I didn't expect so I tried testing with
> this code..
<snip>
> So between two calls to the timer function QueryPerformanceCounter() I
> have made a call to sleep(5) and so the calculated time should be 0.005
> i.e. 5 milliseconds.
> But for the first 5 runs of this program I got the following values for
> duration and none was nowhere near 0.005:-
> 0.002048 s
> 0.006090 s
> 0.008316 s
> 0.006245 s
> 0.009147 s

Which version of Windows are you using? If it's Win9x then the above
figures aren't out of the question, since the timing of the Sleep API is
fairly random at that level on Win9x. You should see similar variation
with larger sleep times. If you're on WinNT the above times are... odd,
to say the least.

The thing to keep in mind is that Sleep() isn't very precise. There are
all sorts of factors that can mess with the timing of it, including
system load and process scheduler settings.

-- 
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"


Relevant Pages

  • Re: How to find the datatype of a variable?
    ... Actually when you're timing operations that take a very small amount of time you need to take into account the effect of the timing mechanism itself. ... On my machine I get a result of 21 microseconds for the complete loop. ... Lib "kernel32" (lpPerformanceCount As Currency) As Long ... QueryPerformanceCounter startTime ' 1st call takes longer ...
    (microsoft.public.vb.general.discussion)
  • Re: Why do windows go blank?
    ... elapsed across the Sleep call is indeed about 100 msec, ... I also minimize the Excel window. ... "kernel32" (ByRef freq As Currency) As Boolean ... Public Declare Function QueryPerformanceCounter Lib "kernel32" _ ...
    (microsoft.public.excel.programming)
  • Re: Sleep(DWORD) returns in _less_ time than specified
    ... through QueryPerformanceCounter and seeing RDTSC - that was a while ago and ... workaround for Sleep returning to soon is to put repeated calls to Sleep ... to QueryPerformanceCounter. ... measurements of PCs using other processors back to a scope. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Why do windows go blank?
    ... How about a little procedure that runs Sleep in a loop with DoEvents inter-mixed? ... Private Declare Sub Sleep Lib "kernel32" ... I also minimize the Excel window. ... Public Declare Function QueryPerformanceCounter Lib "kernel32" _ ...
    (microsoft.public.excel.programming)
  • Re: How to get 1 ms delay ?
    ... to next loop, without any sleep. ... QueryPerformanceCounter(StartTime) ... Sleep(20 - ActalTime) ... In any situation, if the event is not set after nSleepValue, waiting ...
    (microsoft.public.vc.mfc)