c/c++计时:gettimeofday()和clock()

参考:https://blog.csdn.net/iLOVEJohnny/article/details/104602557
https://blog.csdn.net/wu694128/article/details/94542858

struct  timeval{
       long  tv_sec;/*秒*/
       long  tv_usec;/*微妙*/
}struct timeval current_time_infer_sub;
struct timeval current_time1_infer_sub1;
gettimeofday (&current_time_infer_sub, NULL);
...
gettimeofday (&current_time1_infer_sub1, NULL);
double time_diff_infer_sub1 = (current_time1_infer_sub1.tv_usec - current_time_infer_sub.tv_usec)/1000.0;
g_print("zl infer_time_sub1 %fms\n", time_diff_infer_sub1);
int i = 100000000;
clock_t start,finish; //定义开始,结束变量
start = clock();//初始化
while( i-- );
finish = clock();//初始化结束时间
double duration = (double)(finish - start) / CLOCKS_PER_SEC;//转换浮点型
printf( "%lf seconds\n", duration );

在多线程情况下,clock统计的是用户和系统时间的总和,会比实际的程序耗时多出额外的统计;

用gettimeofday获取不同时间段的wall time,然后做差可以得出精确为微秒的耗时。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值