3

I have:

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

DateTime start  = DateTime.Now;
Thread.Sleep(5000);

stopWatch.Stop();
DateTime stop = DateTime.Now;

//stopWatch.ElapsedMilliseconds approximately equals to (stop - start).TotalMilliseconds

So is there a difference in these measurement methods?

ren
  • 3,843
  • 9
  • 50
  • 95

3 Answers3

4

From msdn, System.DateTime:

The Now property is frequently used to measure performance. However, because of its low resolution, it is not suitable for use as a benchmarking tool. A better alternative is to use the Stopwatch class.

Amy B
  • 108,202
  • 21
  • 135
  • 185
3

Stopwatch is more precised than the calculation of (stop - start).TotalMilliseconds.

Check this post on SO.

Community
  • 1
  • 1
Otiel
  • 18,404
  • 16
  • 78
  • 126
3

Stopwatch uses/is a high performance counter which can measure one-hundredth of a millisecond if you don't query the integer ".Millisecond" property but the floating point ".Ellapsed.Totalmilliseconds" property.

springy76
  • 3,706
  • 2
  • 24
  • 46