Export (0) Print
Expand All

Stopwatch.Frequency Field

Note: This field is new in the .NET Framework version 2.0.

Gets the frequency of the timer as the number of ticks per second. This field is read-only.

Namespace: System.Diagnostics
Assembly: System (in system.dll)

public static readonly long Frequency
public static final long Frequency
public static final var Frequency : long

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.

The following example displays the frequency and resolution of the Stopwatch class.

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 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}", 
        (Int64)frequency);
    long nanoSecPerTick = (1000L * 1000L * 1000L) / frequency;
    Console.WriteLine("  Timer is accurate within {0} nanoseconds", 
        (Int64)nanoSecPerTick);
} //DisplayTimerProperties

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft