C# – Quick and Easy Debugging StopWatch

C# – Quick and Easy Debugging StopWatch

Here’s a quick and easy way to create a “stopwatch” object that you can put in a using() {} block as follows. I just needed something quick and dirty to dump time trial results for some of my tests, so I built this IDisposable extension class. Ya, it’s simple, but sometimes that’s handy:

 using(DebugStopWatch stopWatch = new DebugStopWatch()  
{
}

Here’s the code:

 public class DebugStopWatch : IDisposable  
{
private DateTime _start;
public DebugStopWatch()
{
_start = DateTime.UtcNow;
System.Diagnostics.Debug.WriteLine("Starting..."));
}
#region IDisposable Members
public void Dispose()
{
TimeSpan ts = DateTime.UtcNow.Subtract(_start);
System.Diagnostics.Debug.WriteLine(String.Format("Done: {1:0.00} sec", _prefix, ts.TotalSeconds));
}
#endregion
}

1 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Post navigation

Visit Us On LinkedinVisit Us On FacebookVisit Us On TwitterVisit Us On YoutubeCheck Our FeedVisit Us On Instagram