And*_*ite 91 .net c# precision datetime time-precision
我在进行一些单元测试时遇到了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();
}
Run Code Online (Sandbox Code Playgroud)
Eri*_*ert 174
为什么DateTime.Now会不像大多数CPU时钟那样精确?
一个好的时钟应该既精确又准确 ; 那些是不同的.正如老笑话所说,停止的时钟每天准确两次,一分钟慢的时钟在任何时候都不准确.但是一分钟慢的时钟总是精确到最近的分钟,而停止的时钟根本没有用处.
为什么DateTime必须精确到,比如微秒,当它不可能精确到微秒?大多数人没有任何精确到微秒的官方时间信号源.因此,在小数位精度后给出六位数,其中最后五位是垃圾就会撒谎.
请记住,DateTime的目的是表示日期和时间.高精度计时并不完全是DateTime的目的; 如你所知,这就是StopWatch的目的.DateTime的目的是表示日期和时间,例如向用户显示当前时间,计算下周二之前的天数等.
简而言之,"几点了?" 并且"这需要多长时间?" 是完全不同的问题; 不要使用旨在回答一个问题的工具来回答另一个问题.
谢谢你的提问; 这将是一篇很好的博客文章!:-)
Ree*_*sey 18
DateTime的精度在某种程度上与其运行的系统有关.精度与上下文切换的速度有关,该速度往往大约为15或16 ms.(在我的系统上,我的测试实际上大约是14毫秒,但我看到一些笔记本电脑的精确度接近35-40毫秒.)
Peter Bromberg写了一篇关于 C#中高精度代码时序的文章,讨论了这一点.
Jim*_*mmy 13
我想要一个精确的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); } }
}
Run Code Online (Sandbox Code Playgroud)