timeval中使用结构 [英] Timeval struct usage

查看:249
本文介绍了timeval中使用结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算需要运行某些功能时,冲进以下code,记录和放大器;输出纳秒一张code的执行时间

和也有

之间的差异

timeval结构timer_spent &安培; 的timeval timer_spent

  / *把这一行在文件的顶部:* /
#包括LT&; SYS / time.h中>/ *将这个权,你要的时间code面前:* /
timeval结构timer_start,timer_end;
函数gettimeofday(安培; timer_start,NULL);/ * C要的时间将这个权利的$ C $后:* /
函数gettimeofday(安培; timer_end,NULL);
双timer_spent = timer_end.tv_sec - timer_start.tv_sec +(timer_end.tv_usec - timer_start.tv_usec);
的printf(花费时间:%.6f \\ n,timer_spent)/1000000.0;

但我需要在纳秒precise时机。我已经了解的timeval结构没有完整的想法。

所以好心帮我家伙.. !!


解决方案

  

但我需要在纳秒precise时机。


然后用

  INT clock_gettime(clockid_t clk_id,结构的timespec * TP);

它返回的时间以秒和纳秒

 结构的timespec {
  time_t的tv_sec; / *秒*的/
  长tv_nsec; / * *纳秒/
};

相关链接:

I needed to calculate the time it takes to run a certain function and ran into the following code,record & output the execution time of a piece of code in nanoseconds

And also is there any difference between

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;

But I need the precise timing in nanoseconds. I've no complete idea about the timeval struct.

So kindly help me guys.. !!

解决方案

But I need the precise timing in nanoseconds.

Then use

int clock_gettime(clockid_t clk_id, struct timespec *tp);

It returns time in seconds and nanoseconds

struct timespec {
  time_t   tv_sec;        /* seconds */
  long     tv_nsec;       /* nanoseconds */
};

Useful links:

这篇关于timeval中使用结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆