Another useful class

I am probably the last person to discover this but only today I discovered the Stopwatch type in the System.Diagnostics namespace. Real nice when you want to determine how long a certain action took [:)]

 

Stopwatch timer = Stopwatch.StartNew();
timer.Start();
// Do some stuff you want to time.
timer.Stop();
Console.WriteLine(timer.Elapsed.ToString());

 

Use the Stopwatch.IsHighResolution to check if the timer is using high resolution of not.

 

Enjoy!

 


2 thoughts on “Another useful class

  1. here is how to format elapsed in hh:mm:ss:mmm

    Dim ts As TimeSpan = timer.Elapsed
    Dim timeElapsed As String = String.Format(“{0:00}:{1:00}:{2:00}.{3:00}”, _
    ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>