用于测量延迟的计时器
在通过 TCP 测量任何协议中的网络延迟(收到确认的时间 - 发送消息的时间)时,您建议使用什么计时器?为什么? 它有什么分辨率? 还有哪些其他优点/缺点?
可选:它是如何工作的?
可选:您不会使用哪种计时器?为什么?
我主要寻找 Windows / C++ 解决方案,但如果您想对其他系统发表评论,请随意这样做。
(目前我们使用 GetTickCount(),但它不是一个非常准确的计时器。)
When measuring network latency (time ack received - time msg sent) in any protocol over TCP, what timer would you recommend to use and why? What resolution does it have? What are other advantages/disadvantages?
Optional: how does it work?
Optional: what timer would you NOT use and why?
I'm looking mostly for Windows / C++ solutions, but if you'd like to comment on other systems, feel free to do so.
(Currently we use GetTickCount(), but it's not a very accurate timer.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我的答案的副本: C++ 计时器函数提供以纳秒为单位的时间
对于 Linux(和 BSD),您要使用 时钟_gettime()。
对于 Windows,您希望使用 QueryPerformanceCounter。 这里有更多关于 QPC
显然有一个已知的 某些芯片组上的 QPC 存在问题,因此您可能需要确保您没有这些芯片组。 此外,某些双核 AMD 也可能会导致问题。 请参阅 sebbbi 的第二篇文章,他在其中指出:
This is a copy of my answer from: C++ Timer function to provide time in nano seconds
For Linux (and BSD) you want to use clock_gettime().
For windows you want to use the QueryPerformanceCounter. And here is more on QPC
Apparently there is a known issue with QPC on some chipsets, so you may want to make sure you do not have those chipset. Additionally some dual core AMDs may also cause a problem. See the second post by sebbbi, where he states:
您提到您使用 GetTickCount(),因此我建议您查看 QueryPerformanceCounter()。
You mentioned that you use GetTickCount(), so I'm going to recommend that you take a look at QueryPerformanceCounter().
rdtsc 指令确实无可替代。 您无法确定 QueryPerformanceCounter 将支持什么分辨率。 有些具有非常大的粒度(低增量率/频率),有些则根本不返回任何内容。
相反,我建议您使用 rdtsc 指令。 它不需要任何操作系统实现,并返回自计算机/处理器/内核加电以来已经过的 CPU 内部时钟周期数。 对于每秒 30 亿次增量的 3 GHz 处理器来说,没有比这更精确的了,不是吗? 该指令适用于从 Pentium 或 Pentium MMX 开始的 x86-32 和 -64。 因此,它也应该可以从 x86 Linux 访问。
stackoverflow.com 上有很多关于它的帖子。 我自己写了几篇...
There is really no substitute for the rdtsc instruction. You cannot be sure of what resolution the QueryPerformanceCounter will support. Some have a very large granularity (low increment rate/frequency), some return nothing at all.
Instead, I recommend you use the rdtsc instruction. It does not require any OS implementation and returns the number of CPU internal clock cycles that have elapsed since the computer/processor/core was powered up. For a 3 GHz processor that's 3 billion increments per second - it doesn't get more precise than that, now does it? This instruction is available for x86-32 and -64 beginning with the Pentium or Pentium MMX. It should therefore be accessible from x86 Linuxes as well.
There are plenty of posts about it here on stackoverflow.com. I've written a few myself ...