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
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.