C语言计算时间函数 & 理解linux time命令的输出中“real”“user”“sys”的真正含义

https://blog.csdn.net/willyang519/article/details/8841208

中午看了下公众号 发现一个 计算命令执行时间的方法..

time comand.sh

就可以 之前还是自己 捯饬了一个计算realtime 的方法..感觉自己好笨..

 

在完成编译原理的实验时,想比较用链表和哈希表共同执行一段程序的时间是否有差异,最开始使用函数如下:

main()
{
  clock_t start = ;
  clock_t end = ;
  double time;
  int i;
  start = clock();   

  yylex();

  end = clock();
  time = (double)(end - start)/CLOCKS_PER_SEC;/*#define CLOCKS_PER_SEC ((clock_t)1000)*/

  printf("\n执行时间%fsecond\n",time);

}

但由于程序太小,时间显示出来全是0.00000……查到可以用Linux附带的函数,所以就尝试了一下,

使用命令:

time ./test.out

结果显示如下:

real    0m0.026s
user    0m0.012s
sys    0m0.008s

下面转载的文章详细地介绍了time出来显示的“real”“user”“sys”的真正含义。

http://laiwei.net/2010/06/19/%E7%90%86%E8%A7%A3linux-time%E5%91%BD%E4%BB%A4%E7%9A%84%E8%BE%93%E5%87%BAwhat-do-real-user-and-sys-mean-2/

Linux中time命令,我们经常用来计算某个程序的运行耗时,用户态cpu耗时,系统态cpu耗时。

例如:

$ time foo
real        0m0.003s
user        0m0.000s
sys         0m0.004s

那么这三个时间都具体代表什么意思呢?

[1] real : 表示foo程序整个的运行耗时,可以理解为foo运行开始时刻你看了一下手表,foo运行结束时,你又看了一下手表,两次时间的差值就是本次real 代表的值

举个极端的例子如下:可以看到real time恰好为2秒。

# time sleep 2

real    0m2.003s

user    0m0.000s

sys     0m0.000s

[2] user   0m0.000s:这个时间代表的是foo运行在用户态的cpu时间,什么意思?

首先,我来讲一下用户态和核心态:

核心态(Kernel Mode):

在内核态,代码拥有完全的,不受任何限制的访问底层硬件的能力。可以执行任意的CPU指令,访问任意的内存地址。内核态通常情况下,都是为那些最底层的,由操作系统提供的,可信可靠的代码来运行的。内核态的代码崩溃将是灾难性的,它会影响到整个系统。

—– In Kernel mode, the executing code has complete and unrestricted access to the underlying hardware. It can execute any CPU instruction and reference any memory address. Kernel mode is generally reserved for the lowest-level, most trusted functions of the operating system. Crashes in kernel mode are catastrophic; they will halt the entire PC.

用户态(User Mode):

在用户态,代码不具备直接访问硬件或者访问内存的能力,而必须借助操作系统提供的可靠的,底层的APIs来访问硬件或者内存。由于这种隔离带来的保护作用,用户态的代码崩溃(Crash),系统是可以恢复的。我们大多数的代码都是运行在用户态的。

—– In User mode, the executing code has no ability to directly access hardware or reference memory. Code running in user mode must delegate to system APIs to access hardware or memory. Due to the protection afforded by this sort of isolation, crashes in user mode are always recoverable. Most of the code running on your computer will execute in user mode.

为什么要区分Kernel Mode 和 User Mode:

隔离保护,使得系统更稳定。

好,讲完用户态和核心态之后,我们来看user time,说过了,这个指的是程序foo运行在用户态的cpu时间,cpu时间不是墙上的钟走过的时间,而是指CPU工作时间。

[3] sys   0m0.004s : 这个时间代表的是foo运行在核心态的cpu时间。

好,讲完上面的这些,我们来看看这三个的关系,这三者之间没有严格的关系,常见的误区有:

误区一: real_time = user_time + sys_time

我们错误的理解为,real time 就等于 user time + sys time,这是不对的,real time是时钟走过的时间,user time 是程序在用户态的cpu时间,sys time 为程序在核心态的cpu时间。

利用这三者,我们可以计算程序运行期间的cpu利用率如下:

%cpu_usage = (user_time + sys_time)/real_time * 100%

如:

# time sleep 2

real     0m2.003s

user    0m0.000s

sys     0m0.000s

cpu利用率为0,因为本身就是这样的,sleep 了2秒,时钟走过了2秒,但是cpu时间都为0,所以利用率为0

误区二:real_time > user_time + sys_time

一般来说,上面是成立的,上面的情况在单cpu的情况下,往往都是对的。

但是在多核cpu情况下,而且代码写的确实很漂亮,能把多核cpu都利用起来,那么这时候上面的关系就不成立了,例如可能出现下面的情况,请不要惊奇。

real 1m47.363s

user 2m41.318s

sys 0m4.013s

误区三:real_time < user_time + sys_time

一般来讲不会有人闯入这个误区^^

[转帖]C语言计算时间函数 & 理解linux time命令的输出中“real”“user”“sys”的真正含义的更多相关文章

  1. [20170705]理解linux su命令.txt

    [20170705]理解linux su命令.txt --//我一般在维护时经常使用root用户登录,然后su - oracle 转到其他用户操作--//一般都加入 - 参数.这个已经成了条件反射.. ...

  2. 理解linux sed命令

    理解linux sed命令(2010-02-27 18:21:20) 标签:linuxshellsed替换 分类:革命本钱 1. Sed简介sed是一种在线编辑器,它一次处理一行内容.处理时,把当 前 ...

  3. 常用C语言time时间函数

    常见的时间函数有time( ).ctime( ).gmtime( ).localtime( ).mktime( ).asctime( ).difftime( ).gettimeofday( ).set ...

  4. C语言 · 计算时间

    算法提高 计算时间   时间限制:1.0s   内存限制:512.0MB      问题描述 给定一个t,将t秒转化为HH:MM:SS的形式,表示HH小时MM分钟SS秒.HH,MM,SS均是两位数,如 ...

  5. c语言随机函数&&时间函数

    c语言中的随机函数为rand(),但是rand生成的值得大小主要相对一个变量才产生的一定有含义的数,这个相对的变量我们可以再srand()函数中进行设置,srand函数是void类型,内部含一个无符号 ...

  6. C语言的时间函数

    下面是C语言的获取本地时间和构造时间进行格式化时间显示输出的相关函数:This page is part of release 3.35 of the Linux man-pages project. ...

  7. Oracle计算时间函数(numtodsinterval、numtoyminterval)

    numtodsinterval(<x>,<c>) ,x是一个数字,c是一个字符串,表明x的单位,这个函数把x转为interval day to second数据类型 常用的单位 ...

  8. Oracle计算时间函数(对时间的加减numtodsinterval、numtoyminterval) (转)

    原文来自:http://blog.itpub.net/756652/viewspace-697256/ 11g interval分区,按天分区,需要用到函数numtodsinterval.   cre ...

  9. R语言日期时间函数

    Sys.Date( ) returns today's date. date() returns the current date and time.# print today's datetoday ...

随机推荐

  1. php md5算法

    <!DOCTYPE html> <html> <body> <?php $str = "Shanghai"; echo md5($str) ...

  2. java输出程序运行时间

    做了一个MapReduce的小练习,想测试一下程序运行时间: 代码: long start = System.currentTimeMillis(); /*运行的程序主体*/ long end = S ...

  3. (转)RL — Policy Gradient Explained

    RL — Policy Gradient Explained 2019-05-02 21:12:57 This blog is copied from: https://medium.com/@jon ...

  4. docker在windows下上传文件到容器

    我的系统是windows10,docker是用DockerToolbox工具安装的,安装完之后会默认挂载Windows的C:/Users目录,在docker里面对应路径是/c/Users,docker ...

  5. colock

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  6. Java之Object对象中的wait()和notifyAll()用法

    用一个例子来说明Object对象中的wait方法和notifyAll方法的使用. 首先定义一个消息类,用于封装数据,以供读写线程进行操作: /** * 消息 * * @author syj */ pu ...

  7. Python3基础 内置函数 id

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  8. 【Linux】Gitlab库已损坏前端显示500错误解决方法

    背景: 在进行gitlab数据迁移之后,所有页面正常访问,唯独在访问项目repo地址时,报500错误 1 查看日志: 命令查看: gitlab-ctl tail 或者手动查看:/var/log/git ...

  9. Laya的屏幕适配,UI组件适配

    参考: 屏幕适配API概述 版本2.1.1.1 目录 一 适配模式 二 UI组件适配 一 适配模式 基本和白鹭的适配模式一样. Laya官方也推荐了竖屏使用fiexedwidth,横屏使用fixedh ...

  10. 转 移动云基于MySQL Galera的PXC运维实战

    ##sample 1 : mysql 监控 1.phpadmin  比较简单,适合上手 2.mysql_web python 写的, https://github.com/ycg/mysql_web/ ...