kasajian
5/20/2014 - 2:31 AM

C Sharp Timer code for performance measuriment

C Sharp Timer code for performance measuriment

//  Stopwatch stopWatch = new Stopwatch();
//  using ( new Timer( stopWatch ) )
//  {
//      Thread.Sleep(1000);
//  }
//  Print Timer.FormatTimeSpan(stopWatch.Elapsed);
//
public class Timer : IDisposable
{
    private Stopwatch stopWatch;

    public Timer(Stopwatch stop)
    {
        this.stopWatch = stop;
        stopWatch.Start();
    }

    public void Dispose()
    {
        stopWatch.Stop();
    }

    public static string FormatTimeSpan(TimeSpan ts)
    {
        return String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
    }
}