分享

微妙级测试程序运行时间类代码实例

 海漩涡 2017-03-06
class Timetest{
public:
Timetest()
{
start = new timeval;
}
int start_test_time()
{    
   gettimeofday(start,0);                    
}

int stop_test_time(const char* info)
{
struct timeval result;
struct timeval stop;
   gettimeofday(&stop,0);
int diff_usec = -1;

/* 计算start位置到当前位置的时间差 */
   if ( start->tv_sec > stop.tv_sec ) 
   {
       printf("qh ---the time is error\n");
       return -1;
   }
   
   if ( (start->tv_sec == stop.tv_sec) && (start->tv_usec > stop.tv_usec) ) 
   {
       printf("qh ---the time is error\n");
       return -1; 
   }
   
   result.tv_sec = ( stop.tv_sec - start->tv_sec ); 
   result.tv_usec = ( stop.tv_usec - start->tv_usec ); 
  
   if (result.tv_usec<0) 
   { 
       result.tv_sec -= 1; 
       result.tv_usec+=1000000; 
   } 
diff_usec = result.tv_usec/1000 + result.tv_sec*1000;
printf("qh diff [%d]ms:%s\n", diff_usec, info);
   return diff_usec;
}

~Timetest()
{
if(NULL != start)
{
delete start;
start = NULL;
}
}
private:
struct timeval *start;
};

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多