其他人发布的关于在循环中重复运行函数的内容是正确的.
For Linux (and BSD) you want to use 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
}
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:
QueryPerformanceCounter()和
EDIT 2013/07/16:
在某些情况下,QPC的疗效似乎存在一些争议,如http://msdn.microsoft.com/en-us/library/windows/desktop/ee417693(v=vs.85).aspx所述
...而QueryPerformanceCounter和QueryPerformanceFrequency通常会调整
然而,这个答案https://stackoverflow.com/a/4588605/34329表明,在Win XP service pack 2之后,QPC应该可以在任何MS操作系统上正常工作.
本文显示,Windows 7可以确定处理器是否具有不变的TSC,如果没有,则返回到外部计时器.http://performancebydesign.blogspot.com/2012/03/high-resolution-clocks-and-timers-for.html个跨处理器同步仍然是一个问题.
其他与计时器有关的精细读数:
有关更多详细信息,请参阅 comments .