代码之家  ›  专栏  ›  技术社区  ›  Andy White

C#日期时间。现在精确

  •  88
  • Andy White  · 技术社区  · 16 年前

    我只是在做一些单元测试时遇到了DateTime.UtcNow的一些意外行为。当您快速连续调用DateTime.Now/UtcNow时,它似乎会在比预期更长的时间间隔内返回相同的值,而不是捕获更精确的毫秒增量。

    我知道有一个秒表类更适合做精确的时间测量,但我很好奇是否有人能在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();
    }
    
    7 回复  |  直到 11 年前
        1
  •  197
  •   Eric Lippert    16 年前

    为什么DateTime.Now的精确度会低于大多数CPU时钟所能处理的?

    一个好的时钟应该两者兼备 精确的 精确的 ; 这些是不同的。正如一个老笑话所说,一个停止的时钟一天准确两次,一个慢一分钟的时钟在任何时候都不准确。但是慢一分钟的时钟总是精确到最近的一分钟,而停止的时钟根本没有有用的精度。

    为什么日期时间应该是 精确的 精确 ,最后五项是 垃圾 会是 说谎

    记住,DateTime的目的是 表示日期和时间

    简而言之,“现在几点了?”和“花了多长时间?”是完全不同的问题;不要使用专门为回答一个问题而设计的工具来回答另一个问题。

    谢谢你的提问;这将是一篇好的博客文章!:-)

        2
  •  18
  •   Peter Mortensen Pieter Jan Bonestroo    11 年前

    DateTime的精度在某种程度上取决于它所运行的系统。精度与上下文切换的速度有关,一般在15或16毫秒左右(在我的系统上,实际距离我的测试大约14毫秒,但我见过一些笔记本电脑的精确度更接近35-40毫秒。)

    彼得·布罗姆伯格写道 an article on high precision code timing 在C#中,讨论了这一点。

        3
  •  12
  •   Jimmy    12 年前

    我想要一个精确的日期时间。现在:),所以我做了这个:

    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); } }
    }
    
        4
  •  6
  •   Kevin Montrose    16 年前

    从…起 MSDN 你会发现 DateTime.Now 有一个

    实际精度取决于硬件。使用该方法可以获得更好的精度 QueryPerformanceCounter .

        5
  •  6
  •   Pang Ajmal PraveeN    6 年前

    除了实际检查.NET源代码之外,Eric Lippert还提供了 a comment on this SO question 说DateTime只能精确到30毫秒左右。用他的话说,不能精确到纳秒级的原因是“不需要”

        6
  •  3
  •   Peter Mortensen Pieter Jan Bonestroo    11 年前

    From MSDN documentation

    此属性的解决方案

    他们还声称Windows NT 3.5及更高版本上的近似分辨率为10ms:)

        7
  •  1
  •   Iman    7 年前

    此属性的分辨率取决于系统计时器,该计时器 取决于底层操作系统。它往往是 在0.5之间 和15毫秒。

    MSDN Link