Profiling .NET Code Snippets using Stopwatch Class in .NET 2.0 Framework

Profiling .NET Code Snippets using Stopwatch Class in .NET 2.0 Framework

by David Hayden ( .NET Developer )

 

The use of the word “profiling” is stretching it a bit as the Stopwatch Class in the .NET 2.0 Framework is essentially for measuring elapsed time between starts and stops of the Stopwatch, but you get the idea.

Although one should profile their applications using a good .NET Profiler:

there are times when you just want to measure the performance of .NET Code Snippets.

 

Stopwatch Class in .NET 2.0 System.Diagnostics

The .NET 2.0 Framework has introduced a new Stopwatch Class in System.Diagnostics that is perfect for taking quick measurements of the speed of your VB or C# code. It is extremely easy to use:

 

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


// Code To Measure


stopwatch.Stop();

TimeSpan ts = stopWatch.Elapsed;

string elapsedTime =
    String.Format("{0:00}:{1:00}:{2:00}.{3:00}", 
    ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds
/10);

 

I have been using the new Stopwatch Class a good portion of the week testing XML Serilialization options ( which I will post about later ) and it has been very helpful for measuring performance.

To learn more about the Stopwatch Class, check out the MSDN Documentation here.

 

Source:  David Hayden ( .NET Developer )

Filed: C# 2.0

 

posted on Friday, May 12, 2006 2:53 PM

Main

David Hayden Google +

David Hayden Twitter

Health & Fitness

JavaScript Patterns Book Review

HTML 5 and CSS3 - Develop with Tomorrow's Standards Today

Professional ASP.NET Design Patterns Book Review

Beginning Mac Programming Book Review

C# in Depth Book Review

ASP.NET MVC

Orchard CMS

Categories