小编Boj*_*jan的帖子

为什么在重复调用clock_gettime时会看到400x异常值时序?

我试图通过使用物理时钟来测量c ++中某些命令的执行时间,但是我遇到了一个问题,即从计算机上的物理时钟读取测量值的过程可能需要很长时间.这是代码:

#include <string>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <time.h>

int main()
{
      int64_t mtime, mtime2, m_TSsum, m_TSssum, m_TSnum, m_TSmax;
      struct timespec t0;
      struct timespec t1;
      int i,j;
      for(j=0;j<10;j++){
      m_TSnum=0;m_TSsum=0; m_TSssum=0; m_TSmax=0;
      for( i=0; i<10000000; i++) {
            clock_gettime(CLOCK_REALTIME,&t0);
            clock_gettime(CLOCK_REALTIME,&t1);
            mtime = (t0.tv_sec * 1000000000LL + t0.tv_nsec);
            mtime2= (t1.tv_sec * 1000000000LL + t1.tv_nsec);

            m_TSsum += (mtime2-mtime);
            m_TSssum += (mtime2-mtime)*(mtime2-mtime);
            if( (mtime2-mtime)> m_TSmax ) { m_TSmax = (mtime2-mtime);}
            m_TSnum++;
      }
      std::cout << "Average "<< (double)(m_TSsum)/m_TSnum
            << " +/- " << …
Run Code Online (Sandbox Code Playgroud)

c++ linux performance x86 clock

6
推荐指数
2
解决办法
408
查看次数

标签 统计

c++ ×1

clock ×1

linux ×1

performance ×1

x86 ×1