Introduction to System.Diagnostics.Stopwatch
There are a number of 3rd party tools that can
help you profile your code and gauge things like the execution time, amount of
memory consumed, number of loops, and other items useful in refactoring your
code for optimal performance. There are even some tools natively available as
part of Microsoft
Visual Studio. These code profiling tools are designed to be used at
development time to help optimize your code. As someone that has done a lot of C#
programming as an asp.net
developer, I find many of my solutions involve calling out to 3rd
party service providers through web services or other types of integrations.
There are often service level agreements with the 3rd parties that
are hard to monitor if you don’t take steps to ensure that there is timing information
baked into your code around the integration point. The
System.Diagnostics.Stopwatch class was designed for just this sort of use. It
is a lightweight class used to track the amount of time elapsed between some
starting and some ending point in time. Let’s take a look at a couple of the
properties and methods of the Stopwatch class:
- Start() – start the stopwatch
- Stop() – stop the stopwatch
- IsRunning – indicator if the stopwatch is running or not
- Elapsed – the TimeSpan measured by the current instance
-
ElapsedMilliseconds – the elapsed time measured by the current
instance in milliseconds -
ElapsedTicks – the elapsed time measured by the current instance
in timer ticks
It is important to note that the System.Threading.Timer
class is often mistaken to provide the type of functionality provided by the
Stopwatch class. The Timer provides a mechanism for executing a method at a
specified interval, which is completely different than tracking the amount of
time elapsed between two points in time.
The following code snippet demonstrates how to wrap a timer
around a block of code in order to track the amount of time elapsed since the
start and ending execution of the particular block of code.
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch(); timer.Start(); for( int loop = 0; loop < 100; loop++; ) { System.Threading.Thread.Sleep(1000); } timer.Stop(); Console.WriteLine("Elapsed time is: {0}", timer.Elapsed);
I commonly wrap code like the example above around all of my
service calls. I use an application setting to control whether the elapsed time
is recorded. If it is enabled, then after the elapsed time is calculated it is
written to a database or other logging location so that the overall performance
of the service call can be monitored and tracked over time. This has proven
invaluable to me as I have worked with various 3rd parties to
improve the responsiveness of their provided services and ensure that the
performance is trending at the target level.
Summary
We took an introductory look at the
System.Diagnostic.Stopwatch class. We explored the value that using the
stopwatch can provide in order to help monitor the performance of 3rd
party services by tracking the time lapse in making calls to the 3rd
party service.
About the Author:
Mark Strawmyer is a Senior Architect of .NET applications
for large and mid-size organizations. Mark is a technology leader with Crowe Horwath LLP in Indianapolis,
Indiana. He specializes in architecture, design and development of
Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for
application development with C# again. You can reach Mark through http://markstrawmyer.com.