Stopwatch.Frequency 字段

定义

获取以每秒刻度数表示的计时器频率。 此字段为只读。

public: static initonly long Frequency;
public static readonly long Frequency;
 staticval mutable Frequency : int64
Public Shared ReadOnly Frequency As Long 

字段值

Int64

示例

以下示例显示计时器的频率和 Stopwatch 分辨率。 此代码示例是为 类提供的较大示例的 Stopwatch 一部分。

void DisplayTimerProperties()
{
   // 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." );
   }

   Int64 frequency = Stopwatch::Frequency;
   Console::WriteLine( "  Timer frequency in ticks per second = {0}", frequency );
   Int64 nanosecPerTick = (1000L * 1000L * 1000L) / frequency;
   Console::WriteLine( "  Timer is accurate within {0} nanoseconds", nanosecPerTick );
}
public static void DisplayTimerProperties()
{
    // 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);
}
Public Shared Sub DisplayTimerProperties()

   ' Display the timer frequency and resolution.
   If Stopwatch.IsHighResolution Then
      Console.WriteLine("Operations timed using the system's high-resolution performance counter.")
   Else
      Console.WriteLine("Operations timed using the DateTime class.")
   End If
   
   Dim frequency As Long = Stopwatch.Frequency
   Console.WriteLine("  Timer frequency in ticks per second = {0}", frequency)
   Dim nanosecPerTick As Long = 1000000000 / frequency
   Console.WriteLine("  Timer is accurate within {0} nanoseconds", nanosecPerTick)

End Sub

注解

计时器频率指示计时器精度和分辨率。 例如,每秒 200 万个时钟周期的计时器频率等于每个时钟周期 500 纳秒的计时器分辨率。 换而言之,由于一秒等于 10 亿个纳秒为单位,每秒 200 万个计时周期的计时器频率相当于每 10 亿的毫微秒,可以进一步简化为每 500 毫微秒 1 个时钟周期的 200 万个刻度。

Frequency该值取决于基础计时机制的解析。 如果已安装的硬件和操作系统支持高分辨率性能计数器,则值 Frequency 反映该计数器的频率。 否则, Frequency 该值基于系统计时器频率。

由于 Stopwatch 频率取决于已安装的硬件和操作系统,因此在系统运行时 Frequency 值保持不变。

适用于

另请参阅