我需要计算运行某个函数所需的时间并遇到以下代码,记录并输出一段代码的执行时间(以纳秒为单位)
而且两者之间有什么区别吗
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: %.6f\n", timer_spent)/1000000.0;
但我需要以纳秒为单位的精确时间。我对 timeval 结构一无所知。
所以请帮助我的家伙.. !!