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 comment:
Post a Comment