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. 李善友《认知升级之第一性原理》--507张PPT全解!_搜狐科技_搜狐网

      http://www.sohu.com/a/151470602_733114

  2. 如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接(转)

    前期准备: 关于什么是Hierarchy Viewer,请查看官方文档:http://developer.android.com/tools/debugging/debugging-ui.html.个 ...

  3. HDU 3976 Electric resistance (高斯消元法)

    Electric resistance Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. nRF51 DK : nRF51822 Development Kit for Bluetooth Smart, ANT and 2.4GHz applications.

    KEY FEATURES • Affordable, Rapid prototyping and development solution for nRF51 Series SoCs • Kit su ...

  5. AE开发中关于 “无法嵌入互操作类型.........请改用适用的接口”问题的解决方法

    最近开始使用VS2010,在引用COM组件的时候,出现了“无法嵌入互操作类型……,请改用适用的接口”的错误提示. 查阅资料,找到解决方案,记录如下: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌 ...

  6. CF330 C. Purification 认真想后就成水题了

    C. Purification time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. sigmod2017.org

    http://sigmod2017.org/sigmod-program/#ssession20

  8. Java 中 byte、byte 数组和 int、long 之间的转换

    Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; ...

  9. [Winform]缓存处理

    摘要 在对winform做的项目优化的时候,首先想到的是对查询,并不经常变化的数据进行缓存,但对web项目来说有System.Web.Caching.Cache类进行缓存,那么winform端该如何呢 ...

  10. win10系统更新不了,总出现错误0xc8000442

    来自 win10系统更新不了,总出现错误0xc8000442,SHIZHI333的回答. 首先卸载制有第三方防护软件管管家类软件,再试试下面的方法:清理一下更新临时文件,具体操作如下: 1.右键点击开 ...