Stopwatch.Frequency Field
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the frequency of the timer as the number of ticks per second. This field is read-only.
public: static initonly long Frequency;
public static readonly long Frequency;
staticval mutable Frequency : int64
Public Shared ReadOnly Frequency As Long
The following example displays the frequency and resolution of a Stopwatch timer. This code example is part of a larger example provided for the Stopwatch class.
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
The timer frequency indicates the timer precision and resolution. For example, a timer frequency of 2 million ticks per second equals a timer resolution of 500 nanoseconds per tick. In other words, because one second equals 1 billion nanoseconds, a timer frequency of 2 million ticks per second is equivalent to 2 million ticks per 1 billion nanoseconds, which can be further simplified to 1 tick per 500 nanoseconds.
The Frequency value depends on the resolution of the underlying timing mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Frequency value reflects the frequency of that counter. Otherwise, the Frequency value is based on the system timer frequency.
Because the Stopwatch frequency depends on the installed hardware and operating system, the Frequency value remains constant while the system is running.
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: