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