Export (0) Print
Expand All

Stopwatch.ElapsedMilliseconds Property

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

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

public long ElapsedMilliseconds { get; }

Property Value

Type: System.Int64
A read-only long integer representing the total number of milliseconds measured by the current instance.

This property represents elapsed time rounded down to the nearest whole millisecond value. For higher precision measurements, use the Elapsed or ElapsedTicks properties.

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.

The following example uses the Stopwatch class to measure the performance of four different implementations for parsing an integer from a string. This code example is part of a larger example provided for the Stopwatch class.

private static void TimeOperations()
{
    long nanosecPerTick = (1000L*1000L*1000L) / Stopwatch.Frequency;
    const long numIterations = 10000;

    // Define the operation title names.
    String [] operationNames = {"Operation: Int32.Parse(\"0\")",
                                   "Operation: Int32.TryParse(\"0\")",
                                   "Operation: Int32.Parse(\"a\")",
                                   "Operation: Int32.TryParse(\"a\")"};


    // Time four different implementations for parsing  
    // an integer from a string.  

    for (int operation = 0; operation <= 3; operation++)
    {
        // Define variables for operation statistics.
        long numTicks = 0;
        long numRollovers = 0;
        long maxTicks = 0;
        long minTicks = Int64.MaxValue;
        int indexFastest = -1;
        int indexSlowest = -1;
        long milliSec = 0;

        Stopwatch time10kOperations = Stopwatch.StartNew();

        // Run the current operation 10001 times. 
        // The first execution time will be tossed 
        // out, since it can skew the average time. 

        for (int i=0; i<=numIterations; i++) 
        {
            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;
            }

            // Skip over the time for the first operation, 
            // just in case it caused a one-time 
            // performance hit. 
            if (i == 0)
            {
                time10kOperations.Reset();
                time10kOperations.Start();
            }
            else 
            {

                // Update operation statistics 
                // for iterations 1-10001. 
                if (maxTicks < ticksThisTime)
                {
                    indexSlowest = i;
                    maxTicks = ticksThisTime;
                }
                if (minTicks > ticksThisTime)
                {
                    indexFastest = i;
                    minTicks = ticksThisTime;
                }
                numTicks += ticksThisTime;
                if (numTicks < ticksThisTime)
                {
                    // Keep track of rollovers.
                    numRollovers ++;
                }
            }
        }  

        // Display the statistics for 10000 iterations.

        time10kOperations.Stop();
        milliSec = time10kOperations.ElapsedMilliseconds;

        Console.WriteLine();
        Console.WriteLine("{0} Summary:", operationNames[operation]);
        Console.WriteLine("  Slowest time:  #{0}/{1} = {2} ticks",
            indexSlowest, numIterations, maxTicks);
        Console.WriteLine("  Fastest time:  #{0}/{1} = {2} ticks",
            indexFastest, numIterations, minTicks);
        Console.WriteLine("  Average time:  {0} ticks = {1} nanoseconds", 
            numTicks / numIterations, 
            (numTicks * nanosecPerTick) / numIterations );
        Console.WriteLine("  Total time looping through {0} operations: {1} milliseconds", 
            numIterations, milliSec);
    }
}

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.

.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

Community Additions

ADD
Show:
© 2014 Microsoft