Tuesday 9 April 2013

Getting Timings in C#: System.Diagnostics.Stopwatch

Use System.Diagnostics.Stopwatch to measure run times.

You can create a Stopwatch amazingly using the StartNew static method that actually returns a Stopwatch object. This also sets the elapsed time property to zero.

When you want to stop the Stopwatch, all you do is call Stop, which tells the Stopwatch to stop measuring elapsed time.

You can get the elapsed time by calling Elapsed which returns a TimeSpan object or ElapsedMilliseconds which returns the milliseconds elapsed as a long data type (which is just "syntactic sugar" for a System.Int64).

No comments: