定时器功能使用C提供时间在纳秒++ [英] Timer function to provide time in nano seconds using C++

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

问题描述

我要计算它花了一个API返回值的时间。
采取这种行动的时间为纳秒的空间。由于API是一个C ++类/函数,我现在用的是timer.h到caculate相同的:

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 nano seconds. As the API is a C++ class/function, I am using the timer.h to caculate the same:

  #include <ctime>
  #include <cstdio>

  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 <<'\n';

      return 0;
  }

以上code给出秒的时间。我如何得到相同的纳米秒,与更多的precision?

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的。在这里,更多的是对 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双
  核心CPU返回任一PC
  芯随机(个人电脑有时
  跳跃有点倒退),除非你
  特别安装AMD双核心驱动力
  包来解决这个问题。我们没有
  发现任何其他双+核心CPU
  遇到类似问题(P4双,P4超线程,
  酷睿2双,酷睿2四核,Phenom四)。

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年7月16日:

它看起来像有关于QPC的在某些情况下药效一些争论在<一说href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ee417693(v=vs.85).aspx\">http://msdn.microsoft.com/en-us/library/windows/desktop/ee417693(v=vs.85).aspx

It looks like there is some controversy on the efficacy of QPC under certain circumstances as stated in 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的答案 http://stackoverflow.com/a/4588605/34329 指出,QPC应该做工精细之后任何MS操作系统Win XP的Service Pack 2的。

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

本文展示了Windows 7可以确定处理器(S)有一个不变的TSC,如果他们不回落到外部定时器。 <一href=\"http://performancebydesign.blogspot.com/2012/03/high-resolution-clocks-and-timers-for.html\">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:

  • https://blogs.oracle.com/dholmes/entry/inside_the_hotspot_vm_clocks
  • http://lwn.net/Articles/209101/
  • http://performancebydesign.blogspot.com/2012/03/high-resolution-clocks-and-timers-for.html
  • QueryPerformanceCounter Status?

查看评论更多的细节。

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

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