Stopwatch..::.Frequency Field
This page is specific to:.NET Framework Version:
2.03.03.54
.NET Framework Class Library
Stopwatch..::.Frequency Field

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)
Syntax

'Usage

Dim value As Long

value = Stopwatch.Frequency


'Declaration

Public Shared ReadOnly Frequency As Long
Remarks

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.

Examples

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.

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



Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Community Content

Higher resolution requires floating-point calculations
Added by:Gorm Vognsen Jensen

Higher resolution requires floating-point calculations, for example (C#):

long frequency = Stopwatch.Frequency;
Console.WriteLine("  Timer frequency in ticks per second = {0}", frequency);
double nanosecPerTick = (1000.0 * 1000.0 * 1000.0) / (double)frequency;
Console.WriteLine("  Timer is accurate within {0:f} nanoseconds", nanosecPerTick);

Example output:

Operations timed using the system's high-resolution performance counter.

Timer frequency in ticks per second = 2400120000

Timer is accurate within 0.42 nanoseconds

© 2010 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View