Stopwatch.StartNew Method

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
Not applicable.

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 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: