C#DateTime.Now precision [英] C# DateTime.Now precision

查看:305
本文介绍了C#DateTime.Now precision的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是跑与DateTime.UtcNow一些意外的行为,同时做一些单元测试。看来,当你调用DateTime.Now / UtcNow快速连续,似乎给你回相同的值更长的时间比预期的时间间隔,而不是获取更多的precise毫秒增量。

我知道有一个秒表类,这将是更适合做precise测量时间,但我很好奇,如果有人可以解释DateTime的这种行为?是否有一个正式的precision记录在案DateTime.Now(在50&NBSP例如,precise来;毫秒)?为什么DateTime.Now应少precise比大多数CPU时钟可以处理?也许这只是设计的最小公分母的CPU?

 公共静态无效的主要(字串[] args)
{
    VAR秒表=新的秒表();
    stopwatch.Start();
    的for(int i = 0; I< 1000;我++)
    {
        变种现在= DateTime.Now;
        Console.WriteLine(的String.Format(
            蜱:{0} \ tMilliseconds:{1},now.Ticks,now.Millisecond));
    }

    stopwatch.Stop();
    Console.WriteLine(Stopwatch.ElapsedMilliseconds:{0},
        stopwatch.ElapsedMilliseconds);

    到Console.ReadLine();
}
 

解决方案
  

为什么DateTime.Now应少precise比大多数CPU时钟可以处理?

一个很好的时钟应该是两个的 precise 准确的;那些是不同的。至于那个老笑话,一个停止的时钟是完全准确的,一天两次,时钟一分钟,慢则从来没有准确的在任何时间。但时钟一分钟,慢的始终是precise到最近分钟,而停止的时钟没有用precision的。

为什么要日期时间为 precise 的,比方说一个微秒时,它不可能是的准确的到微秒?大多数人没有任何来源的官方时间信号精确到微秒。因此之后的 precision 的,过去的五年,其中小数位给六位的垃圾的是躺在的。

记住,日期时间的目的,是的再present日期和时间的。高precision时机是不是在日期时间的一切的目的;当你注意,这就是秒表的目的。日期时间的目的,是要重新present一个日期和时间为目的,如显示当前时间到用户,计算的天数,直到下一个星期二,依此类推。

总之,现在是什么时候?和多长时间没那个时间?是完全不同的问题;不要使用旨在回答一个问题来回答对方的工具。

谢谢你的问题;这将使一个很好的博客文章! : - )

I just ran into some unexpected behavior with DateTime.UtcNow while doing some unit tests. It appears that when you call DateTime.Now/UtcNow in rapid succession, it seems to give you back the same value for a longer-than-expected interval of time, rather than capturing more precise millisecond increments.

I know there is a Stopwatch class that would be better suited for doing precise time measurements, but I was curious if someone could explain this behavior in DateTime? Is there an official precision documented for DateTime.Now (for example, precise to within 50 ms?)? Why would DateTime.Now be made less precise than what most CPU clocks could handle? Maybe it's just designed for the lowest common denominator 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();
}

解决方案

Why would DateTime.Now be made less precise than what most CPU clocks could handle?

A good clock should be both precise and accurate; those are different. As the old joke goes, a stopped clock is exactly accurate twice a day, a clock a minute slow is never accurate at any time. But the clock a minute slow is always precise to the nearest minute, whereas a stopped clock has no useful precision at all.

Why should the DateTime be precise to, say a microsecond when it cannot possibly be accurate to the microsecond? Most people do not have any source for official time signals that are accurate to the microsecond. Therefore giving six digits after the decimal place of precision, the last five of which are garbage would be lying.

Remember, the purpose of DateTime is to represent a date and time. High-precision timings is not at all the purpose of DateTime; as you note, that's the purpose of StopWatch. The purpose of DateTime is to represent a date and time for purposes like displaying the current time to the user, computing the number of days until next Tuesday, and so on.

In short, "what time is it?" and "how long did that take?" are completely different questions; don't use a tool designed to answer one question to answer the other.

Thanks for the question; this will make a good blog article! :-)

这篇关于C#DateTime.Now precision的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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