C#DateTime.Now精度我在进行一些单元测试时遇到了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();}
3 回答
ITMISS
TA贡献1871条经验 获得超8个赞
DateTime的精度在某种程度上与其运行的系统有关。精度与上下文切换的速度有关,该速度往往大约为15或16 ms。(在我的系统上,我的测试实际上大约是14毫秒,但我看到一些笔记本电脑的精确度接近35-40毫秒。)
Peter Bromberg写了一篇关于 C#中高精度代码时序的文章,讨论了这一点。
潇湘沐
TA贡献1816条经验 获得超6个赞
我想要一个精确的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); } }}
- 3 回答
- 0 关注
- 1727 浏览
添加回答
举报
0/150
提交
取消