Monday, October 12, 2009

Simple way to check time consumed by a piece of code using stopwatch class

TimeSpan ts;
string elapsedTime;
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
//DO SOME LENGTHY OPERATION
ts = stopWatch.Elapsed;
elapsedTime =
                    String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                    ts.Hours, ts.Minutes, ts.Seconds,
                    ts.Milliseconds / 10);
 stopWatch.Reset();
 stopWatch.Stop();

No comments: