Export (0) Print
Expand All

Stopwatch.ElapsedTicks Property

Gets the total elapsed time measured by the current instance, in timer ticks.

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

public long ElapsedTicks { get; }
/** @property */
public long get_ElapsedTicks ()

public function get ElapsedTicks () : long

Not applicable.

Property Value

A read-only long integer representing the total number of timer ticks measured by the current instance.

This property represents the number of elapsed ticks in the underlying timer mechanism. A tick is the smallest unit of time that the Stopwatch timer can measure. Use the Frequency field to convert the ElapsedTicks value into a number of seconds.

You can query the properties Elapsed, ElapsedMilliseconds, and ElapsedTicks while the Stopwatch instance is running or stopped. The elapsed time properties steadily increase while the Stopwatch is running; they remain constant when the instance is stopped.

By default, the elapsed time value of a Stopwatch instance equals the total of all measured time intervals. Each call to Start begins counting at the cumulative elapsed time; each call to Stop ends the current interval measurement and freezes the cumulative elapsed time value. Use the Reset method to clear the cumulative elapsed time in an existing Stopwatch instance.

NoteNote:

Stopwatch ticks are different from DateTime.Ticks. Each tick in the DateTime.Ticks value represents one 100-nanosecond interval. Each tick in the ElapsedTicks value represents the time interval equal to 1 second divided by the Frequency.

The following example uses the Stopwatch class to measure the performance of four different implementations for parsing an integer from a string.

long ticksThisTime = 0;
int inputNum;
Stopwatch timePerParse;

switch (operation)
{
    case 0:
        // Parse a valid integer using
        // a try-catch statement.

        // Start a new stopwatch timer.
        timePerParse = Stopwatch.StartNew();

        try 
        {
            inputNum = Int32.Parse("0");
        }
        catch (FormatException)
        {
            inputNum = 0;
        }

        // Stop the timer, and save the
        // elapsed ticks for the operation.

        timePerParse.Stop();
        ticksThisTime = timePerParse.ElapsedTicks;
        break;
    case 1:
        // Parse a valid integer using
        // the TryParse statement.

        // Start a new stopwatch timer.
        timePerParse = Stopwatch.StartNew();

        if (!Int32.TryParse("0", out inputNum))
        { 
            inputNum = 0;
        }

        // Stop the timer, and save the
        // elapsed ticks for the operation.
        timePerParse.Stop();
        ticksThisTime = timePerParse.ElapsedTicks;
        break;
    case 2:
        // Parse an invalid value using
        // a try-catch statement.

        // Start a new stopwatch timer.
        timePerParse = Stopwatch.StartNew();

        try 
        {
            inputNum = Int32.Parse("a");
        }
        catch (FormatException)
        {
            inputNum = 0;
        }

        // Stop the timer, and save the
        // elapsed ticks for the operation.
        timePerParse.Stop();
        ticksThisTime = timePerParse.ElapsedTicks;
        break;
    case 3:
        // Parse an invalid value using
        // the TryParse statement.

        // Start a new stopwatch timer.
        timePerParse = Stopwatch.StartNew();

        if (!Int32.TryParse("a", out inputNum))
        { 
            inputNum = 0;
        }

        // Stop the timer, and save the
        // elapsed ticks for the operation.
        timePerParse.Stop();
        ticksThisTime = timePerParse.ElapsedTicks;
        break;

    default:
        break;
}

long ticksThisTime = 0;
int inputNum = 0;
Stopwatch timePerParse;

switch (operation) {
    case 0:
        // Parse a valid integer using
        // a try-catch statement.
        // Start a new stopwatch timer.
        timePerParse = Stopwatch.StartNew();

        try {
            inputNum = Int32.Parse("0");
        }
        catch (FormatException exp) {
            inputNum = 0;
        }
        // Stop the timer, and save the
        // elapsed ticks for the operation.
        timePerParse.Stop();
        ticksThisTime = timePerParse.get_ElapsedTicks();
        break;

    case 1:
        // Parse a valid integer using
        // the TryParse statement.
        // Start a new stopwatch timer.
        timePerParse = Stopwatch.StartNew();

        if (!(Int32.TryParse("0", inputNum))) {
            inputNum = 0;
        }
        // Stop the timer, and save the
        // elapsed ticks for the operation.
        timePerParse.Stop();
        ticksThisTime = timePerParse.get_ElapsedTicks();
        break;

    case 2:
        // Parse an invalid value using
        // a try-catch statement.
        // Start a new stopwatch timer.
        timePerParse = Stopwatch.StartNew();

        try {
            inputNum = Int32.Parse("a");
        }
        catch (FormatException exp) {
            inputNum = 0;
        }
        // Stop the timer, and save the
        // elapsed ticks for the operation.
        timePerParse.Stop();
        ticksThisTime = timePerParse.get_ElapsedTicks();
        break;

    case 3:
        // Parse an invalid value using
        // the TryParse statement.
        // Start a new stopwatch timer.
        timePerParse = Stopwatch.StartNew();

        if (!(Int32.TryParse("a", inputNum))) {
            inputNum = 0;
        }
        // Stop the timer, and save the
        // elapsed ticks for the operation.
        timePerParse.Stop();
        ticksThisTime = timePerParse.get_ElapsedTicks();
        break;

    default:
        break;
}

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft