Quick and Easy Code Timing: The .NET Stopwatch Class

   Posted by: Rich Franzmeier

Introduction

The .NET Stopwatch class is nothing new to .NET (it’s been around since version 2.0) but it may be one of those classes you weren’t aware of (I wasn’t aware of it and I’ve been programming .NET since 1.1!)  It is very useful when you want to do some performance testing in your code or to get timings for whatever reason.

The Stopwatch Class

Quick facts:

Examples

The classic example simply creates an instance of the Stopwatch class, calls the Start() method, does the work and finally calls the Stop() method.

Stopwatch sw = new Stopwatch();
sw.Start();

// Do some work...
Thread.Sleep(2155);

sw.Stop();

Console.WriteLine("Elapsed time to do some work: {0}", sw.Elapsed.ToString());

 

Here is the output:

Stopwatch1

You can also instantiate a Stopwatch class using the StartNew() static method of the Stopwatch class.  This method creates a new Stopwatch instance, sets the elapsed time property to zero and starts the watch.

Stopwatch sw = Stopwatch.StartNew();

// Do some work...
Thread.Sleep(2155);

sw.Stop();

Console.WriteLine("Elapsed time to do some work: {0}", sw.Elapsed.ToString());

The output of course is the same as the previous example.

If you need to know if the stopwatch is ticking, use the IsRunning property.

Conclusion

The Stopwatch class is a useful tool to have when you want to do some simple performance testing in your code.  It’s easy to understand and code.


Comments (1)

ASP.NET Development 12/22/2011 4:58 AM

What a great write up.
Thanks for the post.

Add a Comment

*

*

Loading

Find Us
Contact Us 651-288-7000 1-800-866-9884
Home | Training | Curriculum | Course Finder | Schedule | Enroll | Twin Cities Java User Group | Consulting | Foundation | Jobs | About Us | Our Story | Press Room | Instructors | President | Map & Directions | Sitemap

Java Training | JSF / Struts / Spring / Hibernate Training | Java Power Tools Training | .NET 4.0 & Visual Studio 2010 Training | Microsoft Web Development Training | Prism / MVVM / MEF Training | .NET 3.5 and Visual Studio 2008 Training | .NET 2.0 and Visual Studio 2003 Training | Cloud Computing Training | Ajax / Web Services / XML Training | Groovy and Grails Training | SQL Server 2012 Training | SQL Server 2008 Training | SQL Server 2005 Training | Mobile Development Training | SharePoint 2010 Training | SharePoint 2007 Training | Agile, Process, Analysis & Design Training | Arch/Design Patterns Training | Microsoft Official Curriculum Training | Web Development Training | Ruby Training | Rational Application Developer (RAD) Training | WebSphere Application Server Training | WebSphere Portal Training | WebLogic Training | Boot Camp Training | Project Management Training | C / C++ Training | Metro / WinRT / Windows 8 Development Training | Retired

Intertech delivers training on-site and virtually serving cities including Phoenix, AZ | San Francisco, CA | Los Angeles, CA | San Diego, CA | San Jose, CA | Washington, DC | Chicago, IL | Orlando, FL | Boston, MA | Duluth, MN | Minneapolis St. Paul, MN | Rochester, MN | Raleigh-Durham, NC | New York, NY | Philadelphia, PA | Austin, TX | Dallas, TX | Houston, TX | Seattle, WA.