linux测量时间的各种方法比较

35 篇文章 0 订阅

http://blog.csdn.net/nanjunxiao/article/details/8963007


1.time_ttime(time_t*t);

time()函数返回自1970年1月1号0时0分0秒到当前所经过的时间,最小时间精度为秒,粗粒度的时间测量可以考虑使用这个函数,因为它够简单。

2.clock_tclock(void);

clock()函数对时间的测量精度可达微妙,但是它返回的值仅表示CPU实际‘工作’消耗的时间,而一个进程消耗的时间大体可分为三类:

CPU时间、I/O时间、等待时间,所以在单cpu机器上,通过clock函数测量的值会比实际过去的墙上时钟时间(wall clock time,

http://en.wikipedia.org/wiki/Wall_clock_time)要小,但在多cpu机器上,由于可以多个cpu并行工作,

即clock时间可能会成倍计算,所以clock又可能反而比消耗的墙上时钟时间要大。

3.intgettimeofday(structtimeval *tv, structtimezone *tz);

struct timeval{

time_t tv_sec;

suseconds_t tv_usec;

};

gettimeofday()函数的时间测量精度同样为微妙,而且它测量的是墙上时钟时间,微妙级粒度的时间测量可以考虑使用这个函数

4.intclock_gettime (clockid_t which_clock, structtimespec *tp);

struct timespec{

time_t tv_sec;

long tv_nsec;

};

clockid_t clk_id用于指定计时时钟的类型,对于我们Programmr以下三种比较常用:

CLOCK_REALTIME, a system-wide realtime clock.
CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.
CLOCK_REALTIME, a system-wide realtime clock.
CLOCK_PROCESS_CPUTIME_ID, high-resolution timer provided by the CPU for each process.
CLOCK_THREAD_CPUTIME_ID, high-resolution timer provided by the CPU for each of the threads.

纳秒级精确计算时间,clock_gettime()函数依赖rt库,所以编译时要链接上该库。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值