C# DateTime.Now 精度
-
22-09-2019 - |
题
我刚刚在进行一些单元测试时遇到了 DateTime.UtcNow 的一些意外行为。看来,当您快速连续调用 DateTime.Now/UtcNow 时,它似乎会在比预期更长的时间间隔内返回相同的值,而不是捕获更精确的毫秒增量。
我知道有一个 Stopwatch 类更适合进行精确的时间测量,但我很好奇是否有人可以在 DateTime 中解释这种行为?是否有 DateTime.Now 的官方精度记录(例如,精确到 50 毫秒以内?)?为什么 DateTime.Now 的精度低于大多数 CPU 时钟可以处理的精度?也许它只是为最低公分母 CPU 设计的?
public static void Main(string[] args)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
for (int i=0; i<1000; i++)
{
var now = DateTime.Now;
Console.WriteLine(string.Format(
"Ticks: {0}\tMilliseconds: {1}", now.Ticks, now.Millisecond));
}
stopwatch.Stop();
Console.WriteLine("Stopwatch.ElapsedMilliseconds: {0}",
stopwatch.ElapsedMilliseconds);
Console.ReadLine();
}
解决方案
为什么会DateTime.Now进行比大多数CPU时钟可处理那么精确?
一个好的时钟应该既是精确和准确;这些是不同的。至于那个老笑话,停止时钟是完全准确的,一天两次,时钟一分钟慢则从未在任何时候准确。但时钟一分钟缓慢始终精确到最接近的分钟,而停止的时钟没有有用的精度在所有的
日期时间为什么应精确到,说一微秒时,它不可能是准确到微秒?大多数人没有为精确到微秒的官方时间信号的任何来源。因此小数点后给予六位数字的精度的,其中最后五都的垃圾的是躺在的。
记住,日期时间的目的,是表示一个日期和时间。高精度计时是不是所有的DateTime的目的;当你注意,这是秒表的目的。日期时间的目的是为了表示用于目的的日期和时间等显示当前时间给用户,计算的天数,直到下周二,等等。
总之,“现在是什么时候?”和“多长时间这样做走?”是完全不同的问题;不使用设计为回答一个问题回答其他的工具。
感谢您的问题;这将使一个好的博客文章! : - )
其他提示
日期时间的精度在某种程度上特定于它正在运行的系统。精度涉及上下文切换,这往往是约15或16毫秒的速度。 (在我的系统,它实际上是来自我的测试14毫秒,但我已经看到一些笔记本电脑在那里它更接近35-40毫秒的精度。)
彼得布朗伯格写在C#上高精度码定时的制品,其中讨论了这
我想精确Datetime.Now :),所以我熟此设置:
public class PreciseDatetime
{
// using DateTime.Now resulted in many many log events with the same timestamp.
// use static variables in case there are many instances of this class in use in the same program
// (that way they will all be in sync)
private static readonly Stopwatch myStopwatch = new Stopwatch();
private static System.DateTime myStopwatchStartTime;
static PreciseDatetime()
{
Reset();
try
{
// In case the system clock gets updated
SystemEvents.TimeChanged += SystemEvents_TimeChanged;
}
catch (Exception)
{
}
}
static void SystemEvents_TimeChanged(object sender, EventArgs e)
{
Reset();
}
// SystemEvents.TimeChanged can be slow to fire (3 secs), so allow forcing of reset
static public void Reset()
{
myStopwatchStartTime = System.DateTime.Now;
myStopwatch.Restart();
}
public System.DateTime Now { get { return myStopwatchStartTime.Add(myStopwatch.Elapsed); } }
}
从 MSDN 你会发现,DateTime.Now
具有近似 10毫秒上的所有NT操作系统的分辨率。
实际的精度是依赖于硬件的。可以使用 QueryPerformanceCounter
来获得更好的精度。