39
$ 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;
}
4

1 回答 1

38

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

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

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.

于 2012-04-22T04:20:12.020 回答