Stopwatch.Frequency 字段

定义

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

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

字段值

示例

以下示例显示计时器的频率和分辨率 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在系统运行时保持不变。

适用于

另请参阅