real time,user time,sys time

程序运行时间的几个衡量标准包括:real time,user time和sys time,即时钟时间、用户CPU时间、系统CPU时间

通过time命令便可获得这些事件,例如

xxl@xxl-pc:~/apue/real_user_sys time$ time sleep 2

real	0m2.004s
user	0m0.000s
sys	0m0.000s
表明运行sleep这个程序总共花了2.004s,用户CPU时间和系统CPU时间都是0

下面来看一下这几个时间的定义:

real time is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).

user time is the amount of CPU time spent in user-mode code (outside the kernel)within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.

sys time is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system callswithin the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CPU time used by the process. See below for a brief description of kernel mode (also known as 'supervisor' mode) and the system call mechanism.


对应的中文翻译是:

real time 是墙上时间(wall clock time),也就是进程从开始到结束所用的实际时间。这个时间包括其他进程使用的时间片和进程阻塞的时间(比如等待I/O完成)。
user time指进程执行用户态代码(核心之外)所使用的时间。这是执行此进程所消耗的实际CPU时间,其他进程和此进程阻塞的时间并不包括在内。
sys time指进程在内核态消耗的CPU时间,即在内核执行系统调用所使用的CPU时间。


按道理说,进程从开始到结束所经过的时间(real time)应该比进程所消耗的用户时间和系统时间之和(user time + sys time)大,因为real time还算上了进程阻塞的时间,这段时间该进程不占用CPU,但是看下面的例子(摘自CSDN论坛的帖子):

jmf@jmf:~/MyProgram/9_26$ time -p man gcc >>gg
real 1.81
user 2.54
sys 0.12

出现了real < user+sys的情况,这是为什么?解释如下:

user+sys will tell you how much actual CPU time your process used. Note that this is across all CPUs, so if the process has multiple threads it could potentially exceed the wall clock time reported byReal. Note that in the output these figures include the User andSys time of all child processes as well, although the underlying system calls return the statistics for the process and its children separately.

The statistics reported by time are gathered from various system calls. 'User' and 'Sys' come fromwait (2) ortimes (2), depending on the particular system. 'Real' is calculated from a start and end time gathered from thegettimeofday (2) call. Depending on the version of the system, various other statistics such as the number of context switches may also be gathered bytime.

On a multi-processor machine, a multi-threaded process or a process forking children could have an elapsed time smaller than the total CPU time - as different threads or processes may run in parallel. Also, the time statistics reported come from different origins, so times recorded for very short running tasks may be subject to rounding errors, as the example given by the original poster shows.


多处理器的系统上,一个进程如果有 多个线程或者有 多个子进程可能导致real time比CPU time(user + sys time)要小,这是因为不同的线程或进程可以并行执行。换句话说,这里的并行是指真正的并行,不是在单CPU上的多线程并发,如果系统只有一个CPU(单核),那么即使多线程,也不会使得real < user + sys。

举个例子,一个纯计算任务,没有系统调用(即没有耗费sys time,只有user time),采用单线程需要执行的时间为8s。那么在一个四核的机器上,采用并行算法,用4个核一起算,理想上如果完全并行,加速比为4,2s内就能完成,那么从开始到结束,real time是2s。user time呢?应该是2s * 4 = 8s. 所以此时会出现real < sys + user的情况。

同样,对于多CPU的机器上的多线程程序,也会有这样的结果。例子如下,不考虑线程安全之类的问题。

//real_less_than_user.cc
#include <pthread.h>
long long  cnt = 0;
void *ThreadFun(void *)
{
	for (long long i = 0; i < 1000000000; i++)
	{
		cnt++;
	}
}

int main()
{
	pthread_t tid;
	pthread_create(&tid, NULL, ThreadFun, NULL);
	ThreadFun(0);
	pthread_join(tid, NULL);
	
	return 0;
}

运行的时间信息为:

xxl@xxl-pc:~/apue/real_user_sys time$ time ./timeTest 
real	0m18.102s
user	0m33.434s
sys	0m0.280s
可见,多线程程序的user time大于real time。由于涉及到系统调用等原因,sys time也有一点点时间开销。


而对于单线程的程序,如下:

//real_more_than_user.cc
long long  cnt = 0;
void *ThreadFun(void *)
{
	for (long long i = 0; i < 1000000000; i++)
	{
		cnt++;
	}
}

int main()
{
	ThreadFun(0);
	return 0;
}
运行的时间信息为:
xxl@xxl-pc:~/apue/real_user_sys time$ time ./timeTest 
real	0m5.995s
user	0m5.944s
sys	0m0.040s
real > user + sys


但是,有时候也会出现如下情况:

xxl@xxl-pc:~/apue/real_user_sys time$ time ./timeTest 
real	0m5.990s
user	0m5.944s
sys	0m0.048s
此时 rea < user + sys,在单线程的情况下也会出现这种情况,不解,还请高人解释



参考:

http://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1

<think>好的,用户问到了“time 是干嘛的?”。首先,我需要确认用户指的是哪个“time”。在编程系统命令中,常见的time可能有两种情况:Unix/Linux系统中的time命令,或者编程语言中的时间处理函数/模块。首先,考虑用户可能是在使用命令行时遇到time命令。比如在终端里运行time ls,这会测量ls命令的执行时间。这时候需要解释time命令的作用,即测量程序执行的时间,包括实际时间、用户CPU时间系统CPU时间。其次,用户可能是在编程中遇到时间处理的问题,比如Python中的time模块,或者JavaScript的Date对象。这时候需要解释如何在代码中获取当前时间、测量执行时间、格式化时间等操作。 但根据用户之前的问题历史,他们之前询问了关于grep命令的优化,可能更倾向于Unix/Linux环境下的time命令。不过为了全面,我应该涵盖两个方面:系统命令的time编程中的时间处理。 接下来,需要详细解释time命令的用途,给出示例,比如在终端中使用time,并解释输出的三个时间值:realusersys。同时,可以提到在编程中如何实现类似的功能,比如Python中使用time模块的time()perf_counter()函数,或者使用timeit模块来测量代码段的执行时间。 另外,用户可能对time的具体使用场景不同工具之间的区别感兴趣。例如,为什么有时候使用time命令,有时候在代码中自己测量时间?或者不同编程语言中处理时间的差异。 还需要注意用户可能的实际需求,比如他们可能想优化程序的执行时间,或者需要记录某些操作的时间戳。这时候需要提供相应的代码示例,展示如何测量函数执行时间,或者如何生成格式化的时间字符串。 最后,根据用户的问题历史,他们之前询问了关于grep命令的优化,可能是在性能调优的上下文中使用time命令来测量不同命令的执行时间,从而比较优化效果。因此,强调time在性能测试中的作用是有必要的。 总结回答的结构:先解释time命令的作用,再展示示例输出,然后介绍编程中的时间处理,给出代码示例,最后提出相关问题,如不同时间测量工具的区别、如何格式化时间等。</think>`time`在不同上下文中有不同的用途,主要分为两类:系统命令编程中的时间处理。 --- ### 一、系统命令 `time` 在Unix/Linux系统中,`time`是一个用于测量命令执行时间的工具。它会输出以下三个时间指标: - **real**:实际耗时(墙钟时间),即命令从开始到结束的总时间。 - **user**:用户态CPU时间,即程序在用户空间运行的时间。 - **sys**:内核态CPU时间,即程序在内核空间运行的时间。 #### 示例: ```bash time ls -l ``` 输出: ``` real 0m0.002s user 0m0.001s sys 0m0.001s ``` --- ### 二、编程中的时间处理 在编程中,`time`通常指与时间相关的函数或模块,例如: - **获取当前时间戳** - **测量代码执行时间** - **格式化时间字符串** #### 示例1:Python中测量代码执行时间 ```python import time start = time.perf_counter() # 高精度计时器 # 模拟耗时操作 time.sleep(2) end = time.perf_counter() print(f"Execution time: {end - start:.2f} seconds") ``` 输出: ``` Execution time: 2.00 seconds ``` #### 示例2:JavaScript中获取当前时间 ```javascript // 获取当前时间戳(毫秒) const timestamp = Date.now(); console.log(timestamp); // 输出:1717041600000 // 格式化时间 const date = new Date(); console.log(date.toISOString()); // 输出:2024-05-30T12:00:00.000Z ``` --- ### 关键点总结 | 场景 | 用途 | 工具/函数 | |---------------|--------------------------------|---------------------------| | 系统命令 | 测量命令执行时间 | `time`命令(如`time ls`) | | 编程时间处理 | 获取时间、计时、格式化 | Python的`time`/`datetime`模块,JS的`Date`对象 | | 性能分析 | 优化代码或命令的性能 | 结合`time`命令代码计时 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值