在Linux时间命令的输出中real,user和sys的含义 [英] The meaning of real, user, and sys in output of Linux time command

查看:1012
本文介绍了在Linux时间命令的输出中real,user和sys的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ time ./Test 

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

以下是程序代码:

#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;
}


推荐答案

联机帮助页 man time ),它指出:


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

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)).

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

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

这篇关于在Linux时间命令的输出中real,user和sys的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆