1
  #include <sys/time.h>

/*
原型: int gettimeofday( struct timeval *tv, struct timezone *tz );
功能: 获取当前精确时间。在一段代码前后分别使用gettimeofday可以计算代码执行时间.
参数: 其参数tv是保存获取时间结果的结构体,参数tz用于保存时区结果(若不使用则传入NULL即可)。
返回值:成功则返回0,失败返回-1,错误代码存于errno

struct timeval {

long tv_sec; // 秒数

long tv_usec; // 微秒数

}


struct timezone{
int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
int tz_dsttime;     /*日光节约时间的状态*/
};


*/

truct timeval tv_begin, tv_end;

gettimeofday(&tv_begin, NULL);

foo();

gettimeofday(&tv_end, NULL);

 

//执行时间(微秒)= 1000000 * (tv_end.tv_sec - tv_begin.tv_sec) + tv_end.tv_usec - tv_begin.tv_usec;

相关文章:

  • 2021-08-21
  • 2022-12-23
  • 2021-12-20
  • 2021-12-19
  • 2021-10-17
  • 2021-10-19
  • 2022-01-02
  • 2021-09-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2021-11-29
  • 2022-01-09
相关资源
相似解决方案