Monday 12 October 2015

ASP.NET Tips #33 - If you must measure small time differences in your code, ensure you use the StopWatch class

DateTime.UtcNow isn't designed for high-precision timing and will often have a resolution over 10ms, making it unsuitable for measuring small periods of time. The StopWatch class is designed for this purpose, although beware of ending up with an entire codebase with StopWatch instrumentation.
using System.Diagnostics;

class Program
{
   static void Main(string[] args)
   {
      Stopwatch sw = Stopwatch.StartNew();
      // Do something here
      sw.Stop();
      Console.WriteLine("Elapsed time: " + sw.Elapsed.ToString());
   }
}

No comments :

Post a Comment