以C#实现高分辨率DateTime.UtcNow最佳方式吗? [英] Best way to implement a high resolution DateTime.UtcNow in C#?

查看:237
本文介绍了以C#实现高分辨率DateTime.UtcNow最佳方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个时间服务将与超过1ms精度报告时间。我想到了一个简单的解决方案是采取初始计量,并用秒表增量添加到它。问题是,这种方法似乎出现分歧,从墙上的时间非常快。例如,下面的代码试图衡量挂钟时间与我的高精度时钟之间的差异:

I am trying to implement a time service that will report time with greater accuracy than 1ms. I thought an easy 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 extremely fast from wall time. For example, the following code attempts to measure the divergence between Wall Time and my High Resolution Clock:

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);
    }
}



我在约2毫秒/分钟的速度发散在相当近期断绝硬件。

I am diverging at a rate of about 2ms/minute on fairly recent sever hardware.

有没有,我不知道那将是更准确的窗户另一次工厂?如果没有,是否有创造一个高分辨率的时钟或我应该使用第三方库更好的办法?

Is there another time facility in windows that I am not aware of that will be more accurate? If not, is there a better approach to creating a high resolution clock or a 3rd party library I should be using?

推荐答案

入门准确的时钟是困难的。秒表具有非常高的分辨率,但它是不准确的,从在芯片组一个信号获得其频率。这在典型的电子零件公差工作。在硬件业务的恶性竞争抢占了保证稳定的频率昂贵的晶体振荡器。

Getting an accurate clock is difficult. Stopwatch has a very high resolution, but it is not accurate, deriving its frequency from a signal in the chipset. Which operates at typical electronic part tolerances. Cut-throat competition in the hardware business preempted the expensive crystal oscillators with a guaranteed and stable frequency.

DateTime.UtcNow是不是所有的或者准确,但它得到帮助。 Windows会定期接触时间服务,默认的是time.windows.com获得高品质的时钟的更新。并用它来重新调整机器的时钟,插入小的调整,以获得时钟赶上或减慢。

DateTime.UtcNow isn't all that accurate either, but it gets help. Windows periodically contacts a time service, the default one is time.windows.com to obtain an update of a high quality clock. And uses it to recalibrate the machine's clock, inserting small adjustments to get the clock to catch up or slow down.

您需要大量的大技巧来得到它精确到一毫秒。其他代码,并与它的代码和数据页页面锁定你只能得到这样的在内核模式下运行,在中断优先级运行,所以它不能获取代码保证捷足先登所以也不会与页面错误命中。商业解决方案使用一个GPS无线电读取的GPS卫星的时钟信号,通过在烘箱中运行,以提供温度稳定性的振荡器备份。读这样的时钟被硬的问题,你没有多大用处了亚毫秒级的时钟源时,使用它可以让你的程序由操作系统捷足先登,正如其获得的时间,而不是开始运行,直到再次〜45毫秒后。或者更糟。

You need lots of bigger tricks to get it accurate down to a millisecond. You can only get a guarantee like that for code that runs in kernel mode, running at interrupt priority so it cannot get pre-empted by other code and with its code and data pages page-locked so it can't get hit with page faults. Commercial solutions use a GPS radio to read the clock signal of the GPS satellites, backed up by an oscillator that runs in an oven to provide temperature stability. Reading such a clock is the hard problem, you don't have much use for a sub-millisecond clock source when your program that uses it can get pre-empted by the operating system just as it obtained the time and not start running again until ~45 msec later. Or worse.

DateTime.UtcNow精确到毫秒15.625和稳定的感谢时间服务更新很长时间了。会低于只是没有多大意义,你不能让你在用户模式下需要利用它执行的保证。

DateTime.UtcNow is accurate to 15.625 milliseconds and stable over very long periods thanks to the time service updates. Going lower than that just doesn't make much sense, you can't get the execution guarantee you need in user mode to take advantage of it.

这篇关于以C#实现高分辨率DateTime.UtcNow最佳方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆