Timing Operations in Visual Studio 2012

Introduction

The Stopwatch class in System.Diagnostics offers a convenient tool for timing operations so that you might determine which of two or more alternative functions might yield the lowest processing time.

This article briefly describes how one might use the class to time two operations to see if one method might be selected over the other method based upon the amount of time required to execute the operation.

Using the Stopwatch class is trivial; much like a stop watch, an instance of the class may be started, stopped, and the elapsed time may be captured at any time – at the end of the entire process or at any point after the Stopwatch has been started. To measure a single operation, create an instance of the Stopwatch, start it, launch the process, and stop the Stopwatch at completion. Taking the elapsed time will give you a fair idea as to how long the process took to complete. Measuring multiple operations in the same way will permit you to compare the time required for each operation.

Aside from the actual mechanics of using the class, there are two read only fields associated with the class that are worthy of note. The first is IsHighResolution and the second is Frequency. IsHighResolution will report whether or not the Stopwatch instance will run with higher precision. Frequency reports the timer frequency as the number of ticks per second. You really don't need to worry about either one to run a simple test. If you are running relatively recent equipment you will likely always find that IsHighResolution will return true.

Attached Project

The attached project contains a single example. It is a Windows forms application that contains a single form and a couple of functions used to make a time based comparison. The form contains three labels. The first reports whether or not the Stopwatch class supports the high resolution counter. The other two labels are used to report the times captured by the two tests. There are two buttons, one to launch the test, and one to exit the application.

The code is fully annotated and you can see what is going on by reading the notes contained within the code.

using System;

using System.Linq;

using System.Windows.Forms;

using System.Diagnostics;

 

namespace SpeedTest

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

 

            // set up the labels for the initial load

            lblTest1.Text = "Test 1:  Waiting...";

            lblTest2.Text = "Test 2:  Waiting...";

 

            // display whether or not the stop watch

            // supports high resolution counters

            if (Stopwatch.IsHighResolution)

                lblHighPrec.Text = "Stopwatch is high precision";

            else

                lblHighPrec.Text = "Stopwatch is not high precision";

        }

 

        private void btnTest_Click(object sender, EventArgs e)

        {

            // create a new instance of stopwatch and start it

            Stopwatch sw1 = new Stopwatch();

            sw1.Start();

            // run the task

            RunTest1();

            //stop the stop watch

            sw1.Stop();

           

            // display the time required by the task to complete

            lblTest1.Text = "Test 1:  Elapsed time was "

                + sw1.Elapsed.Milliseconds.ToString() + " msec";

           

            // repeat the steps with the second task

            // so we can compare times

            Stopwatch sw2 = new Stopwatch();

            sw2.Start();

            RunTest2();

            sw2.Stop();

 

            lblTest2.Text = "Test 2:  Elapsed time was "

                + sw2.Elapsed.Milliseconds.ToString() + " msec";

           

        }

 

 

        /// <summary>

        /// Meaningless test 1

        /// </summary>

        public void RunTest1()

        {

            lblTest1.Text = "Test 1:  Running...";

 

            for (int i = 0; i < 100000; i++)

            {

                int test = int.Parse("105532");

            }

        }

 

 

        /// <summary>

        /// Meaningless test 2

        /// </summary>

        public void RunTest2()

        {

            lblTest2.Text = "Test 2:  Running...";

 

            for (int i = 0; i < 100000; i++)

            {

                int test = Convert.ToInt32("105532");

            }

        }

 

 

        private void btnExit_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }

    }

}


Similar Articles