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.
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.
public partial class Form1
// 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
lblHighPrec.Text = "Stopwatch is high precision";
lblHighPrec.Text = "Stopwatch is not high precision";
// create a new instance of stopwatch and start it
sw1 = new Stopwatch();
// run the task
//stop the stop watch
// 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
sw2 = new Stopwatch();
lblTest2.Text = "Test 2: Elapsed time was "
+ sw2.Elapsed.Milliseconds.ToString() + " msec";
Meaningless test 1
lblTest1.Text = "Test 1: Running...";
i = 0; i < 100000; i++)
test = int.Parse("105532");
Meaningless test 2
lblTest2.Text = "Test 2: Running...";
i = 0; i < 100000; i++)
test = Convert.ToInt32("105532");