IsHighResolution

  • IsHighResolution 属性指示计时器是基于高分辨率性能计数器还是基于 DateTime 类。
  • 该字段是只读的。
// Display the timer frequency and resolution.
if (Stopwatch.IsHighResolution)
{
    Console.WriteLine("Operations timed using the system's high-resolution performance counter.");
}
else 
{
    Console.WriteLine("Operations timed using the DateTime class.");
}

long frequency = Stopwatch.Frequency;
Console.WriteLine("  Timer frequency in ticks per second = {0}",
    frequency);
long nanosecPerTick = (1000L*1000L*1000L) / frequency;
Console.WriteLine("  Timer is accurate within {0} nanoseconds", 
    nanosecPerTick);
}

https://dotnetfiddle.net/ckrWUo

秒表类使用的计时器取决于系统硬件和操作系统。如果秒表计时器基于高分辨率性能计数器,则 IsHighResolution 为真。否则,IsHighResolution 为 false,表示秒表计时器基于系统计时器。

秒表中的嘀嗒声是机器/操作系统相关的,因此你不应该指望两个系统之间秒表滴答的比例与秒相同,甚至可能在重启后在同一系统上。因此,你永远不能指望秒表刻度与 DateTime / TimeSpan 刻度相同。

要获得与系统无关的时间,请务必使用秒表的 Elapsed 或 ElapsedMilliseconds 属性,这些属性已经考虑了 Stopwatch.Frequency(每秒滴答数)。

秒表应该总是在 Datetime 上用于计时过程,因为它更轻量级并且如果它不能使用高分辨率性能计数器则使用 Dateime。

资源