Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are some other possibilities.</p> <h2>QueryPerformanceCounter and QueryPerformanceFrequency</h2> <p><a href="http://msdn.microsoft.com/en-us/library/ms644904.aspx" rel="noreferrer">QueryPerformanceCounter</a> will return a "performance counter" which is actually a CPU-managed 64-bit counter that increments from 0 starting with the computer power-on. The frequency of this counter is returned by the <a href="http://msdn.microsoft.com/en-us/library/ms644905.aspx" rel="noreferrer">QueryPerformanceFrequency</a>. To get the time reference in <strong>seconds</strong>, divide performance counter by performance frequency. In Delphi: </p> <pre><code>function QueryPerfCounterAsUS: int64; begin if QueryPerformanceCounter(Result) and QueryPerformanceFrequency(perfFreq) then Result := Round(Result / perfFreq * 1000000); else Result := 0; end; </code></pre> <p>On multiprocessor platforms, QueryPerformanceCounter <strong>should</strong> return consistent results regardless of the CPU the thread is currently running on. There are occasional problems, though, usually caused by bugs in hardware chips or BIOSes. Usually, patches are provided by motherboard manufacturers. Two examples from the MSDN:</p> <ul> <li><a href="http://support.microsoft.com/kb/895980" rel="noreferrer">Programs that use the QueryPerformanceCounter function may perform poorly in Windows Server 2003 and in Windows XP</a></li> <li><a href="http://support.microsoft.com/kb/274323" rel="noreferrer">Performance counter value may unexpectedly leap forward</a></li> </ul> <p>Another problem with QueryPerformanceCounter is that it is quite slow.</p> <h2>RDTSC instruction</h2> <p>If you can limit your code to one CPU (SetThreadAffinity), you can use <a href="http://en.wikipedia.org/wiki/RDTSC" rel="noreferrer">RDTSC</a> assembler instruction to query performance counter directly from the processor.</p> <pre><code>function CPUGetTick: int64; asm dw 310Fh // rdtsc end; </code></pre> <p>RDTSC result is incremented with same frequency as QueryPerformanceCounter. Divide it by QueryPerformanceFrequency to get time in seconds.</p> <p>QueryPerformanceCounter is much slower thatn RDTSC because it must take into account multiple CPUs and CPUs with variable frequency. From <a href="http://blogs.msdn.com/oldnewthing/archive/2008/09/08/8931563.aspx" rel="noreferrer">Raymon Chen's blog</a>:</p> <blockquote> <p>(QueryPerformanceCounter) counts elapsed time. It has to, since its value is governed by the QueryPerformanceFrequency function, which returns a number specifying the number of units per second, and the frequency is spec'd as not changing while the system is running. </p> <p>For CPUs that can run at variable speed, this means that the HAL cannot use an instruction like RDTSC, since that does not correlate with elapsed time. </p> </blockquote> <h2>timeGetTime</h2> <p><a href="http://msdn.microsoft.com/en-us/library/ms713418.aspx" rel="noreferrer">TimeGetTime</a> belongs to the Win32 multimedia Win32 functions. It returns time in milliseconds with 1 ms resolution, at least on a modern hardware. It doesn't hurt if you run timeBeginPeriod(1) before you start measuring time and timeEndPeriod(1) when you're done.</p> <h2>GetLocalTime and GetSystemTime</h2> <p>Before Vista, both <a href="http://msdn.microsoft.com/en-us/library/ms713418.aspx" rel="noreferrer">GetLocalTime</a> and <a href="http://msdn.microsoft.com/en-us/library/ms724390.aspx" rel="noreferrer">GetSystemTime</a> return current time with millisecond precision, but they are not accurate to a millisecond. Their accuracy is typically in the range of 10 to 55 milliseconds. (<a href="http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx" rel="noreferrer">Precision is not the same as accuracy</a>) On Vista, GetLocalTime and GetSystemTime both work with 1 ms resolution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload