Thread.sleep代码(1)需要1ms以上 [英] Thread.Sleep(1) takes longer than 1ms

查看:753
本文介绍了Thread.sleep代码(1)需要1ms以上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了这个问题,但没有看到一个答案。如果是重复的,我会很乐意将其关闭。

I searched this question but didn't see an answer. If it is a duplicate I'll gladly close it.

我目前正在做一个技术的一些性能评价,看到一些相当令人难以置信的结果,所以我决定尝试一下。在此,我想试试,看看秒表类是回到了我的预期。

I am currently trying to do some performance evaluation on a technology and saw some rather unbelievable results so I decided to experiment some. In that I wanted to try and see if the Stopwatch class was returning what I expected.

Stopwatch sw = Stopwatch.StartNew();
Thread.Sleep(1);
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);

在这种情况下,我是pretty的多少看到了15ms的返回值。我明白日期时间的分辨率大约有,但不应该的Thread.Sleep(1)睡眠线程时间为1ms?该系统我的回报Stopwatch.IsHighResolution真实,它在.NET 4运行

In this case I was pretty much seeing a return value of 15ms. I understand the resolution of DateTime to be around there but shouldn't Thread.Sleep(1) sleep a thread for 1ms? The system I am on returns Stopwatch.IsHighResolution true and its running in .NET 4.

背景: 这code在其完整的适当形式,旨在收集塞式上的一些数字DB GET请求。该数据库是不是在同一个盒子。当我打印出来的sw.ElapsedMilliseconds当查询在中间我看到大部分子毫秒级响应,这听起来有点可疑考虑到我的Java相当于code为回归更为可信5毫秒,15毫秒响应的大部分时间。 Java的code使用System.nanoTime的差异()。通过在我的C#code submilli回应我的意思是Console.WriteLine(sw.ElapsedMilliseconds)打印0。

Background: This code in its complete and proper form is intended to gather some numbers on Aerospike db get requests. The DB is not on the same box. When I printed out the sw.ElapsedMilliseconds when a query was in the middle I'm seeing mostly sub millisecond responses and that sounds a little suspect considering my Java equivalent code is returning much more believable 5ms-15ms responses most of the time. The Java code is using the difference of System.nanoTime(). By submilli responses in my C# code I mean Console.WriteLine(sw.ElapsedMilliseconds) prints 0.

推荐答案

计时器的的比秒表由时钟递增中断。在默认情况下蜱每秒6​​4次的Windows。或者15.625毫秒。因此,一个Thread.sleep()方法的参数小于16不给你你正在寻找的延迟,您总能获得至少是15.625间隔。同样的,如果你读,说,Environment.TickCount或DateTime.Now并等待的的超过16毫秒,那么你会读相同的值回想起0毫秒已经过去了。

Timers other than Stopwatch are incremented by the clock interrupt. Which by default ticks 64 times per second on Windows. Or 15.625 milliseconds. So a Thread.Sleep() argument less than 16 doesn't give you the delay you are looking for, you'll always get at least that 15.625 interval. Similarly, if you read, say, Environment.TickCount or DateTime.Now and wait less than 16 millisecond then you'll read the same value back and think 0 msec has passed.

请务必使用秒表为小增量的测量,它使用不同的频率源。其分辨率是可变的,它取决于主板上的芯片组。但是,你可以依靠它比一微秒更好。 Stopwatch.Frequency给你的速度。

Always use Stopwatch for small increment measurements, it uses a different frequency source. Its resolution is variable, it depends on the chipset on the motherboard. But you can rely on it being better than a microsecond. Stopwatch.Frequency gives you the rate.

时钟中断率的可以的改变,你要的PInvoke timeBeginPeriod()。这可以让你到一个单一毫秒,实际上使Thread.sleep代码(1)准确。最好不要这样做,这是非常不友好的力量。

The clock interrupt rate can be changed, you have to pinvoke timeBeginPeriod(). That can get you down to a single millisecond and actually make Thread.Sleep(1) accurate. Best not to do this, it is very power unfriendly.

这篇关于Thread.sleep代码(1)需要1ms以上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆