说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix2/timing.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
17.10. Timing
用户常常想收集程序的计时信息。例如,用户可能想测量对程序的某个改动是否提高了它的性能。time 命令是收集进程计时信息的一种方式。它接收一个命令行参数,即要计时的程序或命令的可执行命令行,并输出该程序或命令执行的计时信息。下面是对程序可执行文件 gol 的一次运行进行计时的例子,它接收两个命令行参数(oscillator_500_500.txt 和 0):
Often users want to collect timing information about their programs. For example, a user may want to measure whether a change to a program improves its performance. The
timecommand is one way to collect timing information about a process. It takes a command line argument that is the executable command line of the program or command to time, and it outputs timing information of the execution of the program or command. Here is an example of timing a run of the program executablegolthat takes two command line arguments (oscillator_500_500.txtand0):
1 | $ time ./gol oscillator_500_500.txt 0 |
计时输出给出三个不同的时间值:real 是总的执行时间,有时称为挂钟时间(wall time),因为它对应于从开始到完成的时间,就好像用一只钟来测量运行时间一样;user 是进程在用户态下执行所花的那部分总时间;sys 是进程在系统态(内核态)下执行所花的那部分时间(关于用户态和系统态的更多信息,参见第 13 章:操作系统)。注意,user 时间加上 sys 时间并不等于 real 时间。这是因为在进程总执行时间的一部分里,它阻塞着,等待 I/O 之类的某些事件,或者等待轮到它被操作系统调度到某个 CPU 核心上运行。进程阻塞时,它不会累积 user 或 sys 时间,但会累积 real 时间。
The timing output is given as three different time values:
realis the total execution time, sometimes referred to as wall time because it corresponds to the time from start to completion as if one was measuring runtime with a clock;useris the portion of total time that the process spent executing in user-mode; andsysis the portion of time that the process spent executing is system-mode (see Chapter 13: The Operating System for more information about user and system mode). Note that theusertime plus thesystime do not add up torealtime. This is because during part of a processes total execution time it is blocked waiting for certain events like I/O or waiting for its turn to be scheduled by the OS to run on a CPU core. When a process is blocked, it does not accumulateuserorsystime, but it does accumulaterealtime.
在这个例子中,被计时的 gol 进程大部分时间都在 CPU 核心上运行(它的 user + sys 时间几乎就是它的全部 real 时间)。对于做大量 I/O 的应用程序,它们的 user + sys 时间可能远小于 real 时间。
In this example, the timed
golprocess spends most of its time running on a CPU core (itsuser + systime is almost all of itsrealtime). For applications that do a lot of I/O, theiruser + systime may be much less than theirrealtime.
time 命令常常与 -p 选项一起使用。它以符合 POSIX 标准的格式显示计时输出,这样在用 -p 运行 time 时,每个系统都以相同的格式显示计时结果。在编写对 time 的输出进行后处理的程序(例如计算一组计时运行的平均值)时,这特别有用,因为这样的程序在不同系统上的行为完全相同。
Often, the
-poption is used with thetimecommand. It displays the timing output in a format that is compliant with a POSIX standard, so that every system displays the timing results in the same format whentimeis run with-p. This is particularly helpful for writing programs that do post-processingtimeoutput (e.g., computing the average over a set of timed program runs) that will work identically across systems.
17.10.1. 参考资料
17.10.1. References
更多信息参见:
For more information see:
time的手册页:man 1 time
- The man page for
time:man 1 time
- cheat-sheets.org 上的最常用的 Unix 命令
- most used Unix commands from cheat-sheets.org
- gnu.org 上的 Bash 参考手册。
- Bash Reference Manual from gnu.org.