我需要计算运行某个函数所需的时间,然后遇到以下代码,记录并输出一段代码的执行时间(以纳秒为单位
也有什么区别吗?
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;但我需要以纳秒为单位的精确时间。我对时间结构没有完全的概念。
所以请大家帮帮我..!!
发布于 2014-02-03 13:37:56
,但是我需要精确的时间,单位是纳秒。
然后使用
int clock_gettime(clockid_t clk_id, struct timespec *tp);它返回以秒和纳秒为单位的时间
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};有用的链接:
https://stackoverflow.com/questions/21520206
复制相似问题