實用工具的主要邏輯是這樣的功能:System.DateTime.Now是給壞的結果
private void Run()
{
DateTime startTime = DateTime.Now;
Prepare();
Search();
Process();
DateTime endTime = DateTime.Now;
TimeSpan duration = endTime.Subtract(startTime);
Console.WriteLine("Run took {0:00}:{1:00}:{2:00}",
(int)duration.TotalHours, duration.Minutes, duration.Seconds);
}
當我運行此我可以用我自己的眼睛看到它正在採取至少5秒(Process()
方法會噴出控制檯輸出,我可以觀察5-6秒的情況)。但它報告「運行了00:00:01」。
我不希望時間有微秒精度,但爲什麼這裏完全不準確?
更新: 以下建議我也跑了StopWatch
同期和反對減去2 DateTime
相比,也調試的代碼。這兩種方法同意一小部分秒......調試器中的StopWatch
有1139毫秒。我的假設是,不知何時寫入控制檯的時間不包括在內,但我無法支持它(或反駁它)。
您是否嘗試過調試它? – 2013-03-13 11:45:32
我想你應該使用[Stopwatch](http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.80%29.aspx),並看看[這裏](http://stackoverflow.com/questions/8754018/recommended-method-to-calculate-the-difference-between-two-timespans/8754105#8754105) – V4Vendetta 2013-03-13 11:45:49
你知道'System.Diagnostics.Stopwatch'類? – spender 2013-03-13 11:45:52