linux c gettimeofday
计算时间差
struct timeval now;
gettimeofday(&now, NULL);
biometric_complete_get_notify_mesg(object, invocation, mesg_nodev,
now.tv_sec, now.tv_usec);
void
biometric_complete_get_notify_mesg (
Biometric *object,
GDBusMethodInvocation *invocation,
const gchar *mesg,
gint64 timestamp_sec,
gint64 timestamp_usec) ;
11
结构timeval的定义为:
strut timeval {long tv_sec; /* 秒数 */long tv_usec; /* 微秒数 */};
可以看出,使用这种方式计时,精度可达微秒,也就是10-6秒。进行计时的时候,我们需要前后调用两次gettimeofday,然后计算中间的差值:
gettimeofday( &start, NULL );
foo();
gettimeofday( &end, NULL );
得到微秒: timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
得到秒: timeuse /= 1000000;