.NET System.Diagnostics.Stopwatch issue (returns values too low)

前端 未结 7 1103
眼角桃花
眼角桃花 2021-02-13 23:14

On my computer the Stopwatch is returning values way too low. For example, 200 ms when I specified Thread.Sleep(1000). The program is supposed to wait 1 second. I a

7条回答
  •  走了就别回头了
    2021-02-14 00:09

    I got this:

    1000 ms for DateTime.Now.Ticks
    0999 ms for Stopwatch.ElapsedTicks
    1000 ms for Stopwatch.ElapsedMilliseconds
    0999 ms for Stopwatch.ElapsedTicks after Reset
    0999 ms for Stopwatch.ElapsedTicks setting ThreadAffinity
    0999 ms for Stopwatch.ElapsedTicks setting ProcessorAffinity (and more)
    

    (Couldn't run the last test)

    On a quad-core i7 machine with .NET4 in Linqpad.

    I only ever tend to use Stopwatch.ElapsedMilliseconds but I've never seen anything odd about it. It does sound like there's something broken about your machine or virtualization platform.

提交回复
热议问题