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

備註

計時器頻率表示計時器精確度和解析度。 例如,每秒 2 百萬個刻度計時器頻率等於每個刻度 500 奈秒的計時器解析度。 換句話說,因為一秒等於 10 億個奈秒,每秒 2 百萬個刻度的計時器頻率相當於每 10 億個奈秒的 2 百萬個刻度,這可以進一步簡化為每 500 奈秒 1 個刻度。

Frequency 取決於基礎計時機制的解析。 如果已安裝的硬體和作業系統支援高解析度效能計數器,則 Frequency 此值會反映該計數器的頻率。 否則,此值 Frequency 會以系統計時器頻率為基礎。

Stopwatch因為頻率取決於已安裝的硬體和作業系統,所以 Frequency 當系統執行時,該值會維持不變。

適用於

另請參閱