gpt4 book ai didi

c# - 为什么 DateTime.Now.Ticks 不一致?

转载 作者:太空狗 更新时间:2023-10-29 23:23:00 26 4
gpt4 key购买 nike

我有一个简单的控制台应用程序,主体如下:

Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());
Console.WriteLine(DateTime.Now.Ticks.ToString());

这些是三个不同运行的输出:

635258949900018675
635258949900028676 // +10001
635258949900028676 // +0
635258949900038677 // +10001
635258949900038677 // +0
635258949900038677 // +0
635258949900038677 // +0
635258949900038677 // +0
635258949900038677 // +0
635258949900038677 // +0

635258949937502423
635258949937512424 // +10001
635258949937512424 // +0
635258949937512424 // +0
635258949937512424 // +0
635258949937522425 // +10001
635258949937522425 // +0
635258949937522425 // +0
635258949937522425 // +0
635258949937522425 // +0

635258961813519906
635258961813529907 // +10001
635258961813529907 // +0
635258961813529907 // +0
635258961813529907 // +0
635258961813539908 // +10001
635258961813539908 // +0
635258961813539908 // +0
635258961813539908 // +0
635258961813539908 // +0

人们可能会猜测这些值不同是正常的,因为 Console.WriteLine 执行之间需要时间。线条之间的间隔是 10001 的常数值,但不知何故,这个值不会在每一步都添加到前一个值。有时是,有时不是。我想知道为什么会这样。

最佳答案

埃里克·利珀特 (Eric Lippert) 表现出色 article关于这个话题。关键引述:

The purpose of the “wall clock” timer is to produce dates and times for typical real-world uses, like “what time does Doctor Who start?” or “when do we change to daylight savings time?” or “show me the documents I edited last Thursday after lunch.” These are not operations that require submicrosecond accuracy.

关于c# - 为什么 DateTime.Now.Ticks 不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21252825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com