> For the complete documentation index, see [llms.txt](https://linux-in-action.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://linux-in-action.gitbook.io/project/xing-neng-fen-xi/time.md).

# time 命令

`time` 命令用于统计命令执行所耗费的时间。所有类 UNIX 系统都包含 `time` 命令，使用这个命令可以统计时间消耗。

## 命令用法

```
$ time [options] COMMAND [arguments]
```

其中 `COMMAND` 是要执行的命令，`arguments` 会作为参数传给 `COMMAND` 命令。

## 常用可选参数

| 参数        | 说明                                                                  |
| --------- | ------------------------------------------------------------------- |
| -f FORMAT | 以 FORMAT 字符串设定显示方式。当这个选项没有被设定的时候，会用系统预设的格式。不过可以用环境变量 `time` 来设定这个格式 |

## 结果解析

```
gackle@machine:/leetcode$ time python heap_sort.py
[1, 1, 7, 11, 23, 29, 32, 35, 76, 93, 123, 6674]

real    0m0.042s
user    0m0.016s
sys     0m0.016s
```

* real：指的是挂钟时间，也就是命令从开始执行到结束的时间。这个短时间包括其他进程所占用的时间片，和进程被阻塞时所花费的时间。
* user：指的是进程花费在用户模式中的 CPU 时间，这是**唯一真正用于执行进程所花费的时间**，其他进程和花费阻塞状态中的时间么有计算在内。
* sys：指的是进程花费在内核模式中的 CPU 时间，代表在内核中执行系统调用所花费的时间，这也是真正由进程使用的 CPU 时间。
