如何使用gettimeofday()来获取运行时间

How to use gettimeofday() to get the run time?

本文关键字:获取 运行时间 何使用 gettimeofday      更新时间:2023-10-16

我的问题是"t1.tv_usec-t0.tv_usec"的含义是什么?我觉得这部分没用,"* 1000000"是什么意思?

gettimeofday(&t0, 0);
/* ... */
gettimeofday(&t1, 0);
long elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;

将总时间转换为μs,因为tv_usec是总时间的微秒部分,tv_sec是秒部分,您需要将秒数乘以1000000来获得它们所代表的μs的数量