您的位置:首页 > 运维架构 > Linux

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

2015-02-28 15:03 441 查看
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库,所以编译时要链接上该库。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: