Export (0) Print
Expand All

Stopwatch.StartNew Method

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

Initializes a new Stopwatch instance, sets the elapsed time property to zero, and starts measuring elapsed time.

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

public static Stopwatch StartNew ()
public static Stopwatch StartNew ()
public static function StartNew () : Stopwatch

Return Value

A Stopwatch that has just begun measuring elapsed time.

This method is equivalent to calling the Stopwatch constructor and then calling Start on the new instance.

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 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:
© 2015 Microsoft