linux计算时间精确到微妙

最近需要测算软件计算FFT所需要的时间,所收集参考的资料:

点击(此处)折叠或打开

  1. #include <stdio.h> 
  2.   #include <stdlib.h> /* 包含标准库头文件 */ 
  3.   #include <sys/time.h> 
  4.     
  5.   int main(int argc, char **argv) 
  6.   { 
  7.   struct timeval start,stop,diff; 
  8.   gettimeofday(&start,0); 
  9.                     //做你要做的事... 
  10.   gettimeofday(&stop,0); 
  11.   timeval_subtract(&diff,&start,&stop); 
  12.                     printf("总计用时:%d 微秒\n",diff.tv_usec); 
  13.   } 
  14.     
  15.   /** 
  16.       * 计算两个时间的间隔,得到时间差 
  17.       * @param struct timeval* resule 返回计算出来的时间 
  18.       * @param struct timeval* x 需要计算的前一个时间 
  19.       * @param struct timeval* y 需要计算的后一个时间 
  20.       * return -1 failure ,0 success 
  21.   **/ 
  22.   int timeval_subtract(struct timeval* result, struct timeval* x, struct timeval* y) 
  23.   { 
  24.         int nsec; 
  25.     
  26.         if ( x->tv_sec>y->tv_sec ) 
  27.                   return -1; 
  28.     
  29.         if ( (x->tv_sec==y->tv_sec) && (x->tv_usec>y->tv_usec) ) 
  30.                   return -1; 
  31.     
  32.         result->tv_sec = ( y->tv_sec-x->tv_sec ); 
  33.         result->tv_usec = ( y->tv_usec-x->tv_usec ); 
  34.     
  35.         if (result->tv_usec<0) 
  36.         { 
  37.                   result->tv_sec--; 
  38.                   result->tv_usec+=1000000; 
  39.         } 
  40.     
  41.         return 0; 
  42.   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值