Stop Watch

November 3rd, 2008  | Tags: ,

This is perfect for timing snippets of your code to check your performance.

using System.Diagnostics;

Stopwatch sw = Stopwatch.StartNew();

ProcessThatNeedsTiming()

sw.Stop

Console.WriteLine("Time elapsed: {0}", sw.Elapsed);

A really nice feature of the Stopwatch class is that it will take advantage of any high precision counters, this basically means that it will try and use the most accurate way of timing.  You can check to see if it using a high precision timer with

Stopwatch.IsHighResolution

I have a few ideas about ways to extend this class to make it even more useful and will post these as time permits.

No comments yet.
TOP