Best way to implement high resolution DateTime.UtcNow in C #?

I am trying to implement a time service that will report time with greater accuracy than 1 ms. I thought a simple solution would be to take an initial measurement and use StopWatch to add a delta to it. The problem is that this method seems to diverge very quickly from the wall time. For example, the following code attempts to measure the discrepancy between Wall Time and my high-resolution watch:

public static void Main(string[] args) { System.Diagnostics.Stopwatch s = new System.Diagnostics.Stopwatch(); DateTime baseDateTime = DateTime.UtcNow; s.Start(); long counter = 0; while(true) { DateTime utcnow = DateTime.UtcNow; DateTime hpcutcnow = baseDateTime + s.Elapsed; Console.WriteLine(String.Format("{0}) DT:{1} HP:{2} DIFF:{3}", ++counter, utcnow, hpcutcnow, utcnow - hpcutcnow)); Thread.Sleep(1000); } } 

I disagree at a speed of about 2 ms / min on fairly recent hardware.

Is there any other time on Windows that I don't know about that would be more accurate? If not, is there a better approach to creating a high-resolution watch or a third-party library that I should use?

+6
c # time stopwatch
source share
2 answers

Obtaining an accurate watch is difficult. The stopwatch has a very high resolution, but it is not accurate, deriving its frequency from the signal in the chipset. Which works with typical tolerances on electronic parts. The predominance of competition in the hardware business has surpassed expensive crystal oscillators with a guaranteed and stable frequency.

DateTime.UtcNow is not so accurate, but it gets help. Windows periodically contacts the time service, the default is time.windows.com to receive a high-quality clock update. And uses it to re-calibrate the machine clock, inserting small settings to make the clock catch up or slow down.

You need a lot of great tricks to get precision to the millisecond. You can get such a guarantee only for code that runs in kernel mode, works with interrupt priority, so it cannot be pre-held by other code and with its locked pages of code and data, so it cannot get into pages with page errors Commercial solutions use GPS radio to read the clock of GPS satellites, backed up by an oscillator that runs in the oven to provide thermal stability. Reading such a watch is a difficult problem, you have little use for a sub-millisecond source of synchronization, when the program you use can get preliminary utilization by the operating system, since it got the time and did not start again until ~ 45 ms later. Or worse.

DateTime.UtcNow accurate to 15.625 milliseconds and stable for very long periods thanks to service time updates. Going below, this does not make much sense, you cannot get the guarantee of fulfillment that you need in user mode in order to use it.

+8
source share

Apparently, in Windows 8 / Server 2012, a new API was added to get high-resolution timestamps, the GetSystemTimePreciseAsFileTime API. I have not had the opportunity to play with this, but it looks promising.

+4
source share

All Articles