Measuring code execution time

A better way would be to use Stopwatch, instead of DateTime differences.

Stopwatch Class – Microsoft Docs

Provides a set of methods and properties that you can use to
accurately measure elapsed time.

// create and start a Stopwatch instance
Stopwatch stopwatch = Stopwatch.StartNew(); 

// replace with your sample code:
System.Threading.Thread.Sleep(500);

stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);

Leave a Comment