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

linux 函数库之 times

2012-04-18 16:05 183 查看
函数名: times

头文件: #include<sys/times>

函数声明: clock_t
times(struct
tms *buf);

man帮助查看: man 2 times

参数介绍:

1. clock_t

typedef long int clock_t

2. tms

struct tms {

    clock_t tms_utime; /* user time */

    clock_t tms_stime; /* system time */

    clock_t tms_cutime; /* user time of children */

    clock_t tms_cstime; /* system time of children */

};

概念:

1.实际时间(real time):从命令行执行到运行终止的消逝时间

2.用户CPU时间(user CPU time):命令执行完成花费的系统CPU时间,即命令在用户态中执行时的总和

3.系统CPU时间(system CPU time):命令执行完成花费的系统CPU时间,即命令在核心态中执行时间的总和。

4.cutime是用户CPU时间+子进程用户CPU时间。cstime同理。

    用户CPU时间和系统CPU时间之和为CPU时间,即命令占用CPU执行的时间总和。实际时间要大于CPU时间,因为Linux是多任务操作系统,往往在执行一条命令时,系统还要处理其他任务。另一个需要注意的问题是即使每次执行相同的命令,所花费的时间也不一定相同,因为其花费的时间与系统运行相关。

    由于tms结构没有提供实际时间,所以times函数返回“实际时间”。事实上,如果想获得某段程序运行的时间,可以由以下代码获得

struct tms tmp;

clock_t begin = times(&tmp);

/* your code */

clock_t end = times(&tmp);

printf("%lf\n", (double) (end-begin)/CLOCKS_PER_SEC );

这个是获得程序的实际运行时间(秒)

其中,CLOCKS_PER_SEC是个宏,定义在bits/time.h,可以这么理解

const long long CLOCKS_PER_SEC = 1000000L;

# define CLOCKS_PER_SEC 1000000l
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux system user 任务 struct