由网友(天空ㄟ是那么美)分享简介:什么是精确测量经过挂钟时间在.NET中最便捷的方式是什么?我在找东西微秒精度如果可能的话(10 ^ -6秒)。What is the most convenient way of accurately measuring elapsed wall clock time in .NET? I'm looking fo...
什么是精确测量经过挂钟时间在.NET中最便捷的方式是什么?我在找东西微秒精度如果可能的话(10 ^ -6秒)。
What is the most convenient way of accurately measuring elapsed wall clock time in .NET? I'm looking for something with microsecond accuracy if possible (10^-6 seconds).
推荐答案
System.Diagnostics.Stopwatch是你最好的选择。然而,确切的精确度将取决于你使用,即高分辨率的性能计数器是否可在计算机上的硬件。 (您可以检查与IsHighResolution场。)
使用示例:
Stopwatch sw = Stopwatch.StartNew();
// Do stuff here
sw.Stop();
TimeSpan time = sw.Elapsed;
请注意,如果你使用的ElapsedTicks属性,在定时器的真实测量蜱这是不一样的的DateTime
使用的蜱时间跨度
。这是抓住了我之前了 - 这就是为什么我总是用ElapsedMilliseconds或Elapsed属性。
Note that if you use the ElapsedTicks
property, that's measured in timer ticks which is not the same as the ticks used in DateTime
and TimeSpan
. That's caught me out before now - which is why I always use ElapsedMilliseconds
or the Elapsed
property.
相关推荐
最新文章