How to use System.Diagnostics.Stopwatch?

Stopwatch is a good guy provided by MS to accurately measure elapsed time.
[sourcecode language=”csharp”]
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
// Your Time Consuming Code goes here..
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
[/sourcecode]
Happy Coding! 🙂

2 Comments

Comments are closed.