Linux中统计程序运行所用时间

#include
#include

#include

int timeval_subtract(struct timeval* diff, struct timeval* start, struct timeval* stop)

int main(int argc, char **argv)
{
  struct timeval start,stop,diff;
  gettimeofday(&start,0);
 //要统计时间的程序,...
  gettimeofday(&stop,0);

  timeval_subtract(&diff,&start,&stop);

 float diffTime=(float)diff.tv_sec+(float)diff.tv_usec/1000;

  printf("diffTime=%f\n",diffTime);

 }

int timeval_subtract(struct timeval* diff, struct timeval* start, struct timeval* stop)
{     
      if ( satrt->tv_sec>stop->tv_sec )
          return -1;
        
      if ( (start->tv_sec==stop->tv_sec) && (start->tv_usec>stop->tv_usec) )
          return -1;
        
      diff->tv_sec = ( stop->tv_sec-start->tv_sec );
      diff->tv_usec = ( stop->tv_usec-start->tv_usec );
        
      if (diff->tv_usec<0)//当 相差超过1s时进行处理
      {
          diff->tv_sec--;
          diff->tv_usec+=1000000;
      }
        
      return 0;
}


你可能感兴趣的:(Linux中统计程序运行所用时间)