BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Performance
.NET 2.0+

Speed Testing and the Stopwatch Class

Software must operate at a speed that is acceptable to the end user. Often large improvements can be made by improving the speed of short-lived but heavily used routines. The speed of these can be accurately measured using the .NET 2.0 Stopwatch class.

Reading the Stopwatch

The sample program now includes all of the additional code required to time the function. The only remaining task is to read the value of the Stopwatch object after stopping the timer. The Stopwatch class provides several properties that provide this information:

  • Elapsed. The Elapsed property returns a TimeSpan object representing the amount of time that the Stopwatch was active. TimeSpan objects are accurate to one tenth of a microsecond or one hundred nanoseconds.
  • ElapsedMilliseconds. The ElapsedMilliseconds property is less accurate than the Elapsed property as it only counts full milliseconds. For longer operations this accuracy can be quite adequate.
  • ElapsedTicks. This property returns the number of timer ticks counted by the Stopwatch. A timer tick is the smallest measurement of time possible for a Stopwatch object. The length of a timer tick can vary according to the specific computer and operating system used. The number of timer ticks per second can be identified by reading the static Frequency property of the Stopwatch class.

The selection of property to use for speed testing is dependant upon the task being measured. In our example, the Elapsed property provides the required level of accuracy and will be used to output the number of microseconds that the timer is active. This will be formatted with an accuracy of one decimal place. This is the highest accuracy possible for the TimeSpan class.

The sample can now be updated with the code required to output the execution time. The following shows the completed program:

using System;
using System.Diagnostics;

namespace StopWatchExample
{
    class Example
    {
        static void Main(string[] args)
        {
            Stopwatch timer = new Stopwatch();
            long total = 0;

            timer.Start();          // Start the timer

            for (int i=1;i<=1000000;i++)
            {
                total += i;
            }
                
            timer.Stop();           // Stop the timer
            
            decimal micro = (decimal)timer.Elapsed.Ticks / 10M;
            Console.WriteLine("Execution time was {0:F1} microseconds.", micro);
        }
    }
}

The sample may now be executed to see the time required to process the totalling operation. As the sample has been created as a console application, hit CTRL-F5 to execute the program. The output to the console will be similar to the following:

Execution time was 1493.7 microseconds.
Press any key to continue . . .
25 November 2006