Linux时间命令输出中real、user、sys的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10265162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:56:02  来源:igfitidea点击:

The meaning of real, user, and sys in output of Linux time command

linuxtime

提问by 2607

$ time ./Test 

real    0m2.906s
user    0m2.887s
sys     0m0.017s

Here is the program code:

下面是程序代码:

#include <iostream>
#include <map>

void func_a() {
    std::map<int, int> m;
    for (unsigned int i = 0; i < 10000; i++) {
        m.insert(std::pair<int, int>(i, i));
    }
}

void func_b() {
    std::map<int, int> m;
    for (unsigned int i = 0; i < 1000000; i++) {
        m.insert(std::pair<int, int>(i, i));
    }
}

int main() {
    func_a();
    func_b();
    return 0;
}

采纳答案by LukeGT

If you take a look at the manpage(man time), it states:

如果您查看联机帮助页( man time),它会指出:

The time command runs the specified program command with the given arguments. When command finishes, time writes a message to standard output giving timing statistics about this program run. These statistics consist of (i) the elapsed real time between invocation and termination, (ii) the user CPU time (the sum of the tms_utime and tms_cutime values in a struct tms as returned by times(2)), and (iii) the system CPU time (the sum of the tms_stime and tms_cstime values in a struct tms as returned by times(2)).

time 命令使用给定的参数运行指定的程序命令。命令完成后,time 会向标准输出写入一条消息,提供有关此程序运行的计时统计信息。这些统计数据包括 (i) 调用和终止之间经过的实时时间,(ii) 用户 CPU 时间(times(2) 返回的结构 tms 中的 tms_utime 和 tms_cutime 值的总和),以及 (iii)系统 CPU 时间(times(2) 返回的结构 tms 中 tms_stime 和 tms_cstime 值的总和)。

Basically though, the usertime is how long your program was running on the CPU, and the systime was how long your program was waiting for the operating system to perform tasks for it. If you're interested in benchmarking, user + sysis a good time to use. realcan be affected by other running processes, and is more inconsistent.

基本上,user时间是您的程序在 CPU 上运行的sys时间,时间是您的程序等待操作系统为其执行任务的时间。如果您对基准测试感兴趣,这user + sys是使用的好时机。 real可能会受到其他正在运行的进程的影响,并且更加不一致。