使用 C++ 以纳秒为单位提供时间的计时器功能 [英] Timer function to provide time in nano seconds using C++

查看:46
本文介绍了使用 C++ 以纳秒为单位提供时间的计时器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望计算 API 返回值所需的时间.执行此类操作所需的时间在几纳秒内.由于 API 是 C++ 类/函数,我使用 timer.h 来计算相同的:

I wish to calculate the time it took for an API to return a value. The time taken for such an action is in the space of nanoseconds. As the API is a C++ class/function, I am using the timer.h to calculate the same:

  #include <ctime>
  #include <iostream>

  using namespace std;

  int main(int argc, char** argv) {

      clock_t start;
      double diff;
      start = clock();
      diff = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;
      cout<<"printf: "<< diff <<'
';

      return 0;
  }

上面的代码给出了以秒为单位的时间.如何在纳秒内以更高的精度获得相同的结果?

The above code gives the time in seconds. How do I get the same in nano seconds and with more precision?

推荐答案

其他人发布的关于在循环中重复运行该函数的内容是正确的.

What others have posted about running the function repeatedly in a loop is correct.

对于 Linux(和 BSD),您想使用 clock_gettime().

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
}

对于想要使用 QueryPerformanceCounter 的 Windows.这里有更多关于 QPC

For windows you want to use the QueryPerformanceCounter. And here is more on QPC

显然,某些芯片组上的 QPC 存在一个已知的问题,因此您可能需要制作确定你没有那些芯​​片组.此外,一些双核 AMD 也可能导致问题.请参阅 sebbbi 的第二篇文章,他指出:

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() 和QueryPerformanceFrequency() 提供了一个更好的分辨率,但有不同的问题.例如在Windows XP,所有 AMD Athlon X2 dual核心 CPU 返回其中任何一个的 PC核心随机"(PC有时向后跳一点),除非你专门安装AMD双核驱动包来解决问题.我们没有注意到任何其他双核 CPU有类似的问题(p4 dual、p4 ht、core2 dual, core2 quad, phenom quad).

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).

编辑 2013/07/16:

http://msdn.microsoft.com/en-us/library/windows/desktop/ee417693(v=vs.85).aspx

...虽然 QueryPerformanceCounter 和 QueryPerformanceFrequency 通常调整为多个处理器、BIOS 或驱动程序中的错误可能会导致这些例程返回随着线程从一个处理器移动到另一个处理器,不同的值...

...While QueryPerformanceCounter and QueryPerformanceFrequency typically adjust for multiple processors, bugs in the BIOS or drivers may result in these routines returning different values as the thread moves from one processor to another...

但是这个 StackOverflow 回答 https://stackoverflow.com/a/4588605/34329 指出 QPC 应该可以正常工作在 Win XP Service Pack 2 之后的任何 MS 操作系统上.

However this StackOverflow answer https://stackoverflow.com/a/4588605/34329 states that QPC should work fine on any MS OS after Win XP service pack 2.

本文表明,Windows 7 可以确定处理器是否具有不变的 TSC,如果没有,则回退到外部计时器.http://performancebydesign.blogspot.com/2012/03/high-resolution-clocks-and-timers-for.html 跨处理器同步仍然是一个问题.

This article shows that Windows 7 can determine if the processor(s) have an invariant TSC and falls back to an external timer if they don't. http://performancebydesign.blogspot.com/2012/03/high-resolution-clocks-and-timers-for.html Synchronizing across processors is still an issue.

与计时器相关的其他精读:

Other fine reading related to timers:

查看评论了解更多详情.

See the comments for more details.

这篇关于使用 C++ 以纳秒为单位提供时间的计时器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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