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

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

问题描述

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

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.

我知道有一个 Stopwatch 类更适合进行精确的时间测量,但我很好奇是否有人可以在 DateTime 中解释这种行为?是否有 DateTime.Now 的官方精度记录(例如,精确到 50 毫秒以内?)?为什么 DateTime.Now 不如大多数 CPU 时钟可以处理的精确?也许它只是为最小公分母 CPU 设计的?

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}	Milliseconds: {1}", now.Ticks, now.Millisecond));
    }

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

    Console.ReadLine();
}

推荐答案

为什么 DateTime.Now 不如大多数 CPU 时钟可以处理的精确?

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.

为什么 DateTime 应该精确到,比如说微秒,而它不可能精确到微秒?大多数人没有任何精确到微秒的官方时间信号的来源.因此,在精度的小数点后给出六位数字,其中最后五位是垃圾将是撒谎.

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.

请记住,DateTime 的目的是表示日期和时间.高精度计时根本不是 DateTime 的目的;正如您所注意到的,这就是 StopWatch 的目的.DateTime 的用途是表示日期和时间,例如向用户显示当前时间、计算到下周二的天数等.

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 精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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