c语言 gettimeofday

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;

原文地址:https://www.cnblogs.com/helloweworld/p/2682485.html