interested in absolute time, rather than a way to measure the duration.
开发者_运维问答Win32 API
c++
You would need special hardware for that. Even though Windows APIs report milliseconds it's not exactly accurate as per this discussion:
Most intel pentium base PC's (I'm not sure about others) have a timer chip on the motherboard that has a 1.19318166667MHz counter.
The counter counts down from N (by default N=65535) to 1 at the rate of 1.19318166667MHz. The system timer interrupt is generated when the counter rolls over from 1 to N (zero never occurs). Note the system time is updated by this interrupt.
If N=65535 then the system timer interrupt is generated (1.19318166667/65535*1000000)=18.2 times per second. This equates to every 54.9 milliseconds.
This grainularity is inherent in most PC's in use today.
You'll need hardware for such kind of absolute accuracies, like a GPS Radio clock. You will also need to write device driver level software, user mode programs cannot nearly respond fast enough to time something down to a millisecond. In general, Windows is not the right kind of operating system for this.
What you have to do is correlate the time of day with a value of an offset counter, and together with the frequency of the counter you can subsequently in your application quickly calculate the absolute time.
For a single core host not on power management and with a stable TSC using RDTSC
would be the fastest, but you would have to determine the frequency yourself, typically by running a fixed time loop of say 5 seconds and measuring the difference.
The HPET device was created to overcome deficiencies of the TSC with multiple cores, hyper-threading, and power management causing variable clock rates. HPETs are only available in modern hardware, they have their own counter and programmable frequency and must be read similar to the TSC. The cost is more expensive, about 500ns though.
Windows provides the functions QueryPerformanceCounter and QueryPerformanceFrequency to handle this all for you, it will automagically choose the APIC or HPET device and use the TSC to interpolate values.
There is something in performance counters that has a 1ms resolution IIRC.
GetSystemTime()
If you want accuracy, you should check out NIST, National Institute of Standards and Technology. They have software that allows you to get the time from their servers. It has high accuracy and resolution. This is also the time standard used by GPS receivers.
However, you may not be able to get the time at a high frequency, due to network traffic and irregularities.
timeGetTime was designed for use in media playback, so it is more accurate than the GetTickCount()
function. It's not calibrated to Clock time however.
To get clock time accurate to the millisecond. You have to get the clock time, and also timeGetTime
or QueryPerformanceCounter
at as close to the same time as possible. Then you can make successive calls to timeGetTime
etc, to get the number of milliseconds that have elapsed time from the known clock time.
Be warned that timeGetTime
wraps around every 47 days ...
精彩评论