在通过 TCP 的任何协议(protocol)中测量网络延迟(接收确认时间 - 发送消息时间)时,您会推荐使用什么计时器?为什么?它有什么分辨率?其他优点/缺点是什么?
可选:它是如何工作的?
可选:您不会使用什么计时器,为什么?
我主要寻找 Windows/C++ 解决方案,但如果您想对其他系统发表评论,请随时发表评论。
(目前我们使用 GetTickCount(),但它不是一个非常准确的计时器。)
最佳答案
这是我的答案的拷贝:C++ Timer function to provide time in nano seconds
对于 Linux(和 BSD),您要使用 clock_gettime() .
#include <sys/time.h>
int main()
{
timespec ts;
// clock_gettime(CLOCK_MONOTONIC, &ts); // Works on FreeBSD
clock_gettime(CLOCK_REALTIME, &ts); // Works on Linux
}
对于要使用 QueryPerformanceCounter 的 Windows .更多关于 QPC 的信息
显然有一个已知的 issue在某些芯片组上使用 QPC,因此您可能需要确保您没有这些芯片组。此外,一些双核 AMD 也可能导致 problem .请参阅 sebbbi 的第二篇文章,他在其中指出:
QueryPerformanceCounter() and QueryPerformanceFrequency() offer a bit better resolution, but have different issues. For example in Windows XP, all AMD Athlon X2 dual core CPUs return the PC of either of the cores "randomly" (the PC sometimes jumps a bit backwards), unless you specially install AMD dual core driver package to fix the issue. We haven't noticed any other dual+ core CPUs having similar issues (p4 dual, p4 ht, core2 dual, core2 quad, phenom quad).
关于c++ - 用于测量延迟的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/305615/