我需要找到一个瓶颈,并且需要准确地测量时间.
以下代码段是衡量性能的最佳方法吗?
DateTime startTime = DateTime.Now;
// Some execution process
DateTime endTime = DateTime.Now;
TimeSpan totalTimeTaken = endTime.Subtract(startTime);
Run Code Online (Sandbox Code Playgroud) 我想跟踪我的代码的性能,所以我使用了存储开始和结束时间System.DateTime.Now.我将两者之间的差异作为执行代码的时间.
我注意到差异似乎并不准确.所以我尝试使用一个Stopwatch对象.事实证明,这更加准确.
谁能告诉我为什么Stopwatch比计算开始和结束时间之间的差异更准确System.DateTime.Now?
顺便说一句,我不是说百分之十.我得到了大约15-20%的差异.