C++ 如何在C++;?

C++ 如何在C++;?,c++,linux,benchmarking,execution-time,C++,Linux,Benchmarking,Execution Time,我正在尝试对一段代码进行基准测试。它基于此代码,有趣的部分基本上如下所示: auto t0 = std::chrono::high_resolution_clock::now(); ... auto t1 = std::chrono::high_resolution_clock::now(); std::cout << secs(t1-t0).count() << " s\n"; auto t0=std::chrono::high_res

我正在尝试对一段代码进行基准测试。它基于此代码,有趣的部分基本上如下所示:

    auto t0 = std::chrono::high_resolution_clock::now();
    ...
    auto t1 = std::chrono::high_resolution_clock::now();
    std::cout << secs(t1-t0).count() << " s\n";
auto t0=std::chrono::high_resolution_clock::now();
...
自动t1=std::chrono::高分辨率时钟::现在();

std::cout理想情况下,正如您问题下的注释所提到的,您应该隔离您试图在
main
中测量的代码片段,只需使用unix
time
命令行工具,它报告
user
system
时间


如果您无法做到这一点,请考虑查找“<代码>时间>代码>,并可能使用相同的技术来对代码进行基准测试。

< P>如果您在操作系统上可用,可以使用与内置的代码>时间< /C>命令类似的结果。

#include <sys/times.h>
...
struct tms  start, end;
times(&start);
//Do command
times(&end);
clock_t usr_time = end->tms_utime - start->tms_utime;
clock_t sys_time = end->tms_stime - start->tms_stime;
#包括
...
结构tms开始、结束;
时间(&开始);
//指挥
时间(&end);
时钟使用时间=结束->tms使用时间-开始->tms使用时间;
时钟系统时间=结束->tms时间-开始->tms时间;

要100%完成,您应该检查时间的结果是否不等于
-1
,否则检查
errno
代码。

我认为您需要使用
clock\u gettime()
和以下时钟
clock\u PROCESS\u CPUTIME\u ID
clock\u THREAD\u CPUTIME\u ID
。它们给出了进程/线程所消耗的CPU时间量

CLOCK_PROCESS_CPUTIME_ID
  High-resolution per-process timer from the CPU.
CLOCK_THREAD_CPUTIME_ID
  Thread-specific CPU-time clock.
参考:

我认为,merlin2011的建议在很多情况下是不可行的,因为隔离可能需要很多努力,而使用
clock\u gettime()
添加两行代码是非常实用的。您甚至可以在一个类中封装对
clock\u gettime()
的调用。

对于Linux

  #include <time.h>
  #include <stdio.h>

  double  theseSecs = 0.0;
  double  startSecs = 0.0;
  double  secs;
  double  CPUsecs = 0.0;
  double  CPUutilisation = 0.0;
  double  answer = 0;
  clock_t starts;

  void start_CPU_time()
  {      
      starts = clock();;
      return;
  }

  void end_CPU_time()
  {
      CPUsecs = (double)(clock() - starts)/(double)CLOCKS_PER_SEC;
      return;
  }    

  struct timespec tp1;
  void getSecs()
  {
     clock_gettime(CLOCK_REALTIME, &tp1);
     theseSecs =  tp1.tv_sec + tp1.tv_nsec / 1e9;           
     return;
  }

  void start_time()
  {
      getSecs();
      startSecs = theseSecs;
      return;
  }

  void end_time()
  {
      getSecs();
      secs    = theseSecs - startSecs;
      return;
  }    

  void calculate()
  {
      int i, j;
      for (i=1; i<100001; i++)
      {
          for (j=1; j<10001; j++)
          {
              answer = answer + (float)i / 100000000.0;
          }
      }
  }

 void main()
 {
     start_time();
     start_CPU_time();
     calculate();
     end_time();
     end_CPU_time();
     CPUutilisation = CPUsecs /  secs * 100.0;
     printf("\n Answer %10.1f, Elapsed Time %7.4f, CPU Time %7.4f, CPU Ut %3.0f%\n",
              answer, secs, CPUsecs, CPUutilisation);  
 }
#包括
#包括
双塞秒=0.0;
双startSecs=0.0;
双秒;
双CPUsecs=0.0;
双CPU利用率=0.0;
双答案=0;
时钟开始;
无效启动\u CPU\u时间()
{      
开始=时钟();;
返回;
}
无效结束\u CPU\u时间()
{
CPUsecs=(双精度)(时钟()-开始)/(双精度)每秒时钟;
返回;
}    
结构timespec tp1;
void getSecs()
{
时钟获取时间(时钟实时和tp1);
这些秒=tp1.tv秒+tp1.tv秒/1e9;
返回;
}
无效开始时间()
{
getSecs();
startSecs=这些秒;
返回;
}
无效结束时间()
{
getSecs();
秒=秒-开始秒;
返回;
}    
无效计算()
{
int i,j;

对于(i=1;i您是否考虑过在
main
中隔离代码、编译并通过运行它?(不是测量小代码段的最佳方法,但由于您使用的是秒,我认为这应该足够好)作为最后的手段,我可以这样做,但是我正在运行大量的测试和大量的配置,如果我必须继续复制到main中,那么这个测试将花费更长的时间,并且当你说“时间”时,它将变得不那么愉快,你的意思是CPU时间吗?如果你要排除所有其他进程对CPU的使用,你想让时间包括i/o吗?在这个特殊情况下没有i/o时间。它只是测试具有不同算法和不同输入的无序映射行为的代码片段。我希望看到一些关于gprof和gcc的提及标记使其工作。为什么不为此使用探查器?请参阅以下问题。答案中有一个打字错误。
start
end
不是指针,因此计算应该是
clock\t usr\u time=end.tms\u utime-start.tms\u utime;
clock\t sys\u time=end.tms\u stime-start.tms\u stime;
此外,单位也要重新计算ned by
times()
是根据
CLOCKS\u per\u second
计算的经过的滴答声的数量。因此,要获得经过的秒数,请执行
(双倍)usr\u time/CLOCKS\u per\u second
(我想)。Kilian请花点时间更正上述错误。