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
头文件: #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系统编程之进程(四):wait/waitpid函数与僵尸进程、fork 2 times
- linux 函数库之 times
- Linux进阶之bash编程(函数及总结)
- linux 函数局部变量的堆栈使用
- 【Linux】shell---函数的简单例子
- linux下c++多线程下用CLOCK()函数统计程序运行时间 2012-12-11 12:46:01 分类: C/C++ clock()函数的功能: 这个函数返回从“开启这个程序进程”到
- Linux下程序设计常用函数
- linux struct utsname 结构详解是utsname 函数使用说明
- Linux下Kill函数用法
- (6)Linux_C_数学计算函数
- Linux 线程操作函数总结
- linux驱动的入口函数module_init的加载和释放
- Linux时间函数
- linux中select()函数分析
- Linux时间函数
- linux下时间有关的函数和结构体
- linux驱动——内核通知链(探究i2c-dev.c 中的bus_register_notifier函数所得)
- 查看Linux下*.a库文件中文件、函数、变量等情况
- Linux 系统库函数coreleft 与sbrk简介
- Linux系统编程——vfork() 函数详解