Tuesday, November 11, 2008 1:08 PM
If you have ever done some profiling or tried to see how fast/slow certain stubs/snippets run, there is a good chance that you have written timers using the timer class that works very much like a stop watch. The idea usually is to see the elapse time of certain piece of snippet which usually helps us developers track down the bottle neck. With .Net 1.1 you pretty much had to write your own stop watch class and there are tons of them out there, Google the term "stopwatch class .net" and you will see for yourself.
Anyway, with C# 2.0+ there is no need to do that anymore as the .Net library is now a bit smarter with its all new Stopwatch class which is a member of the System.Diagnostics namespace. Here is a snippet of how it could be used, have fun.
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
// Code snippet here
// blah blah blah
watch.Stop();
TimeSpan ts = watch.Elapsed;
string elapse = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes,
ts.Seconds, ts.Milliseconds / 10);