API Temporary Resolution for GetLocalTime ()

I need to find out the time that a function performs in my application. The application is a solution of MS VIsual Studio 2005, all C-codes.

I used the tht windows API GetLocalTime (SYSTEMTIME *) to get the current system time before and after calling the function I want to measure. But this has the disadvantage that its low resolution is only 1 ms. Nothing below. Therefore, I cannot get the granularity of time in microseconds.

I know that the time (), which gives the time elapsed since the era, also has a resolution of 1 ms (without microseconds)

1.) Is there any other Windows API that gives microsecond time that I can use to measure the time it takes for my function?

-AD

+6
c profile
source share
4 answers

There are other possibilities.

QueryPerformanceCounter and QueryPerformanceFrequency

The QueryPerformanceCounter will return a "performance counter", which is actually a 64-bit counter with a processor that increments from 0, starting from the computer’s power - on. The frequency of this counter is returned by QueryPerformanceFrequency . To get a time reference in seconds , divide the performance counter by the frequency of performance. In Delphi:

function QueryPerfCounterAsUS: int64; begin if QueryPerformanceCounter(Result) and QueryPerformanceFrequency(perfFreq) then Result := Round(Result / perfFreq * 1000000); else Result := 0; end; 

On multiprocessor platforms, QueryPerformanceCounter should return consistent results regardless of which processor is currently running. Sometimes problems arise, usually caused by errors in the hardware chips or BIOS. As a rule, patches are provided by motherboard manufacturers. Two examples from MSDN:

Another problem with the QueryPerformanceCounter is that it is rather slow.

RDTSC Instruction

If you can limit your code to a single processor (SetThreadAffinity), you can use the RDTSC instruction to request a performance counter directly from the processor.

 function CPUGetTick: int64; asm dw 310Fh // rdtsc end; 

The RDTSC result is incremented at the same rate as the QueryPerformanceCounter. Divide it by QueryPerformanceFrequency to get the time in seconds.

QueryPerformanceCounter is much slower than RDTSC because it has to consider multiple processors and processors with variable frequency. From the Raymon Chen Blog :

(QueryPerformanceCounter) counts elapsed time. It should, because its value is controlled by the QueryPerformanceFrequency function, which returns a number indicating the number of units per second, and the frequency is not indicated when the system is running.

For processors that can operate at variable speed, this means that the HAL cannot use an instruction like RDTSC, since this does not correlate with elapsed time.

timeGetTime

TimeGetTime refers to Win32 media functions of Win32. It returns time in milliseconds with a resolution of 1 ms, at least on modern hardware. This will not hurt if you run timeBeginPeriod (1) before you start measuring the time and time EndPeriod (1) when you are done.

GetLocalTime and GetSystemTime

Prior to Vista, both GetLocalTime and GetSystemTime return the current millisecond accurate, but they are not accurate to the millisecond. Their accuracy is usually in the range of 10 to 55 milliseconds. ( Accuracy does not match accuracy ) In Vista, GetLocalTime and GetSystemTime work with a resolution of 1 ms.

+10
source share

Multiprocessor Warning:

from http://msdn.microsoft.com/en-us/library/ms644904(VS.85).aspx

On a multiprocessor computer, it should not matter which processor is being called. However, you may get different results on different processors due to errors in the basic input / output system (BIOS) or the hardware abstraction layer (HAL). To indicate the affinity of a processor for a thread, use the SetThreadAffinityMask function.

Al weiner

+2
source share

You can try using clock () , which will provide the number of ticks between two points. A tick is the smallest unit of time that a processor can measure.

As a side note, you cannot use clock () to determine the actual time — only the number of ticks between two points in your program.

+1
source share

On Windows, you can use the high performance counter API. Check out: QueryPerformanceCounter and QueryPerformanceCounterFrequency for details.

+1
source share

All Articles