我需要计算运行某个功能所需的时间,然后遇到以下代码,记录&输出纳秒中的代码的执行时间
,
之间也有任何区别 struct timeval timer_spent
&timeval timer_spent
/* Put this line at the top of the file: */
#include <sys/time.h>
/* Put this right before the code you want to time: */
struct timeval timer_start, timer_end;
gettimeofday(&timer_start, NULL);
/* Put this right after the code you want to time: */
gettimeofday(&timer_end, NULL);
double timer_spent = timer_end.tv_sec - timer_start.tv_sec + (timer_end.tv_usec - timer_start.tv_usec);
printf("Time spent: %.6fn", timer_spent)/1000000.0;
,但我需要纳秒中的精确时机。我对TimeVal结构没有完整的想法。
好友帮助我.. !!
,但我需要纳秒中的精确计时。
然后使用
int clock_gettime(clockid_t clk_id, struct timespec *tp);
它在秒内返回时间和纳秒
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
有用的链接:
- http://linux.die.net/man/3/clock_gettime
- 使用clock_getres -newbie linux c