获取进程执行时间

一、时间概念

  在linux下进行编程时,可能会涉及度量进程的执行时间。linux下进程的时间值分三种:

    时钟时间(real time):指进程从开始执行到结束,实际执行的时间。

    用户CPU时间(user CPU time):指进程中执行用户指令所用的时间,也包括子进程。

    系统CPU时间(system CPU time):指为进程执行内核程序所经历的时间,例如调用read和write内核方法时,消耗的时间就计入系统CPU时间。

二、获取方法  

  有两种方法可以获取,第一种是用time命令,time 进程。第二种是通过在程序中进行记录,首先利用sysconf函数获取时钟滴答数,再用times获取tms结构。

查看times函数,man 2 tms,得到tms结构定义和times函数声明如下:

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 */
};
#include <sys/times.h>

 clock_t times(struct tms *buf);

注意:此处计算的时间是时钟滴答数,需要除以系统时钟滴答数,得出实际的秒数。

三、测试例子

测试程序如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
#include <unistd.h> #define BUFFER_SIZE 4 * 1024 int main()
{
int sc_clk_tck;
sc_clk_tck = sysconf(_SC_CLK_TCK); struct tms begin_tms, end_tms;
clock_t begin, end;
system("date");
begin = times(&begin_tms);
sleep();
end = times(&end_tms); printf("real time: %lf\n", (end - begin) / (double)sc_clk_tck);
printf("user time: %lf\n", (end_tms.tms_utime - begin_tms.tms_utime) / (double)sc_clk_tck);
printf("sys time: %lf\n", (end_tms.tms_stime - begin_tms.tms_stime) / (double)sc_clk_tck);
printf("child user time: %lf\n", (end_tms.tms_cutime - begin_tms.tms_cutime) / (double)sc_clk_tck);
printf("child sys time: %lf\n", (end_tms.tms_cstime - begin_tms.tms_cstime) / (double)sc_clk_tck);
return ;
}

测试结果如下所示:

采用time命令,测试结果如下所示:

其中real表示时钟时间,user表示用户CPU时间,sys表示系统CPU时间。time命令也可以用于系统的命令,如time ls、time ps等等。

参考:http://www.cnblogs.com/Anker/p/3416288.html

linux --> 获取进程执行时间的更多相关文章

  1. Linux获取进程执行时间

    1.前言    测试一个程序的执行时间,时间包括用户CPU时间.系统CPU时间.时钟时间.之前获取之前时间都是在程序的main函数用time函数实现,这个只能粗略的计算程序的执行时间,不能准确的获取其 ...

  2. Unix/Linux获取进程的详细信息

    Linux的进程的信息都记录在/proc/<pid>/下面,其实常用的ps.top命令也是从这里读取信息的.常用的信息有: cmd(命令).cmdline(完整的命令行参数).envrio ...

  3. Linux获取进程中变量

    列出所有进程 #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> ...

  4. Windows与Linux获取进程集合的方法

    Windows: List<String> tasklist=new ArrayList<String>(); try { Process process = Runtime. ...

  5. Linux常用获取进程占用资源情况手段

    测试环境:Ubuntu14.04 1.  获取进程ID号 ps -aux | grep your_process_name 例如: xxx@xxx:~$ ps -e |grep Midlet|awk ...

  6. linux: 获取监听指定端口的进程PID

    在 linux 下经常需要杀死(重启)监听某端口的进程, 因此就写了一个小脚本, 通过 ss 命令获取监听制定端口的进程 PID, 然后通过 kill 命令结束掉进程: #!/bin/sh # set ...

  7. linux中使用top获取进程的资源占用信息

    在linux中使用top获取进程的资源占用信息: Cpu(s):  1.0%us,  0.0%sy,  0.0%ni, 98.3%id,  0.7%wa,  0.0%hi,  0.0%si,  0.0 ...

  8. linux命令(26):Bash Shell 获取进程 PID

    转载地址:http://weyo.me/pages/techs/linux-get-pid/ 根据pid,kill该进程:http://www.cnblogs.com/lovychen/p/54113 ...

  9. linux shell 获取进程pid

    1.通过可执行程序的程序名称 a.运行程序 b.获取进程id号 c.pidof相关知识:http://www.cnblogs.com/yunsicai/p/3675938.html 2.有些程序需要在 ...

随机推荐

  1. WaitForSingleObject函数

    WaitForSingleObject函数 VC声明 DWORD WaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds ); VB声明 D ...

  2. 在U-boot中添加以太网驱动

    当定义CONFIG_CMD_NET和CONFIG_CMD_PING,编译之后执行ping命令,告警没有找到以太网. 因此,需要打开U-boot的网络功能, u-boot-sunxi-sunxi中没有找 ...

  3. Flex中的FusionCharts 3D饼图

    1.3D饼图设计源码 <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns: ...

  4. HighCharts之2D条状图

    HighCharts之2D条状图 1.HighCharts之2D条状图源码 bar.html: <!DOCTYPE html> <html> <head> < ...

  5. VME总线

    简介 诞生于25年前的VME(VersaModule Eurocard)总线是一种通用的计算机总线,结合了Motorola公司Versa总线的电气标准和在欧洲建立的Eurocard标准的机械形状因子, ...

  6. ssh_Connection reset by peer报错

    连接SSH时,产生了一下错误----->Read from socket failed: Connection reset by peer 首先查看日志 tail -f /var/log/aut ...

  7. 关于这个该死的报错:TypeError - 'undefined' is not a function (evaluating '_getTagName(currWindow).toLowerCase()')

    在利用Selenium爬取页面信息的时候突然报错,第一条信息爬取的时候还好好的,第二条就不行了. 请参考网上的爬取代码: # coding=utf-8"""Created ...

  8. RAM

    1.     前记 我们知道,不同的计算机结构对RAM 的使用方式是有区别的,典型的计算机结构有两个,冯诺依曼结构和哈佛结构,而两大阵营的领军人物就是传说中的Intel X86系列的8086和51单片 ...

  9. RobotFramework下的http接口自动化Set Request Header 关键字的使用

    Set Request Header 关键字用来设置http请求时的请求头部信息. 该关键字接收两个参数,[ header_name | header_value ] 示例1:设置http请求时的Re ...

  10. jxl 导出数据到excel

    优点: Jxl对中文支持非常好,操作简单,方法看名知意. Jxl是纯javaAPI,在跨平台上表现的非常完美,代码可以再windows或者Linux上运行而无需重新编写 支持Excel 95-2000 ...