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

前端 未结 7 1135
眼角桃花
眼角桃花 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条回答
  •  旧时难觅i
    2021-02-14 00:06

    Seems like you're using Tick count in some cases. Remember that by default on modern Windows for example, the OS will save CPU. This means that the tick count and the time elapsed are not in linear proportion.

    I suggest you try using the Stopwatch.ElapsedMilliseconds in the most basic form:

    var sw = new Stopwatch();
    sw.Start();
    Thread.Sleep(1000);
    var elpased = sw.Elapsed;
    

提交回复
热议问题