1、前言

      测试一个程序的执行时间,时间包括用户CPU时间、系统CPU时间、时钟时间。之前获取之前时间都是在程序的main函数用time函数实现,这个只能粗略的计算程序的执行时间,不能准确的获取其他时间。在看《APUE》时,书中有关程序时间测试程序,非常正规,提供这三个时间。如是,上网搜了一下,进行总结一下。

2、获取方法

  有两种方法可以获取,第一种是用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);

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

3、测试例子:

测试程序如下:

   #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命令,测试结果如下所示:

4、参考网址

http://www.01happy.com/linux-process-time/

http://www.01happy.com/c-get-process-time/

Linux获取进程执行时间的更多相关文章

  1. linux --> 获取进程执行时间

    获取进程执行时间 一.时间概念 在linux下进行编程时,可能会涉及度量进程的执行时间.linux下进程的时间值分三种: 时钟时间(real 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. 利用dynamic简化数据库的访问

    今天写了一个数据库的帮助类,代码如下. public static class DbEx { public static dynamic ReadToObject(this IDataReader r ...

  2. EasyNetQ介绍

    EasyNetQ 是一个容易使用,坚固的,针对RabbitMQ的 .NET API. 假如你尽可能快的想去安装和运行RabbitMQ,请去看入门指南.EasyNetQ是为了提供一个尽可能简洁的适用与R ...

  3. 使用Gitblit 搭建Windows Git服务器

    使用Gitblit 搭建Windows Git服务器 整理使用Gitblit搭建Git服务器的步骤. 目录 使用Gitblit 搭建Windows Git服务器 目录 下载安装 配置 运行 客户端运行 ...

  4. C#访问远程主机资源的方法,多种方式

    最近要实现访问远程主机的共享目录中的一个文件.遇到了权限问题.google了一下,找到了几种解决方法,记录如下: 一.调用Net use命令 // 使用方法:        //if (Connect ...

  5. swift笔记(二) —— 运算符

    基本运算符 Swift支持大部分的标准C语言的操作符,而且做了一些改进,以帮助开发人员少犯低级错误,比方: 本该使用==的时候,少写了个=, if x == y {-} 写成了 if x = y {- ...

  6. A look at WeChat security

    原文地址:http://blog.emaze.net/2013/09/a-look-at-wechat-security.html TL;DR: Any (unprivileged) applicat ...

  7. Android上的单元测试

    Android上的单元测试 http://www.sina.com.cn  2009年12月04日 16:07  IT168.com [IT168 技术文档]任何程序的开发都离不开单元测试来保证其健壮 ...

  8. (转载):ASCII,Unicode和UTF-8 编码

    UTF-8是Unicode的一种实现方式,也就是它的字节结构有特殊要求,所以我们说一个汉字的范围是0X4E00到0x9FA5,是指unicode值,至于放在utf-8的编码里去就是由三个字节来组织,所 ...

  9. Office Web Apps 错误日志

    前言 最近一直在用Office Web Apps,使用过程会有各种各样的错误,众所周知,sharepoint的错误都在15/Logs下面保存错误日志,那么OWA呢? 经过查找,发现Office Web ...

  10. 疑犯追踪第五季/全集Person of Interest迅雷下载

    英文全名Person of Interest,第5季(2015)CBS.本季看点:<疑犯追踪>本季剧组暗示Finch可能重建机器,这次他会给机器更多自由(如Root一直要求的那样).或许新 ...