回到手册索引

命令用途

time 命令用于测量 Linux 命令的执行时间,输出其实际运行时间(real)、用户态 CPU 时间(user)和内核态 CPU 时间(sys),帮助分析命令性能。

常用用法示例

  1. 基本用法:测量命令执行时间

    1
    2
    3
    4
    time ls -l
    real 0m0.003s
    user 0m0.001s
    sys 0m0.002s

    测量 ls -l 命令的执行时间,输出各阶段耗时。

  2. 测量长时间运行的命令

    1
    2
    3
    4
    time sleep 1
    real 0m1.002s
    user 0m0.001s
    sys 0m0.001s

    测量 sleep 1 的执行时间,real 接近 1 秒,而 CPU 占用极低。

  3. 将输出重定向到文件

    1
    2
    3
    4
    5
    /usr/bin/time -o output.txt ls
    output.txt 内容

    0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 1784maxresident)k
    0inputs+0outputs (0major+76minor)pagefaults 0swaps

    使用 /usr/bin/time 并将结果保存到 output.txt。

  4. 测量脚本执行时间

    1
    2
    3
    4
    time ./script.sh
    real 0m0.123s
    user 0m0.045s
    sys 0m0.078s

    测量 Shell 脚本的总执行时间。

  5. 结合管道命令测量

    1
    2
    3
    4
    time find / -name "*.txt" 2>/dev/null | wc -l
    real 0m2.456s
    user 0m0.234s
    sys 0m0.567s

    测量管道命令的总耗时(包含 find 和 wc)。

  6. 多次运行取平均时间

    1
    2
    /usr/bin/time --verbose -f "%E" sleep 0.5 # 多次手动运行后计算
    0:00.50

    需手动多次运行取平均值(示例为单次输出)。

  7. 测量内存使用情况

    1
    2
    /usr/bin/time -v python3 script.py
    Maximum resident set size (kbytes): 25600 # 输出片段

    使用 -v 参数显示内存占用等详细信息。

  8. 与 timeout 命令结合使用

    1
    2
    time timeout 2s dd if=/dev/zero of=/dev/null
    real 0m2.003s # 2秒后被终止

    测量被 timeout 限制的命令执行时间。

常用参数选项

  • -p, –portability
    使用 POSIX 兼容格式输出,例如 real 0.05。
  • -v, –verbose
    显示详细信息(如内存、I/O 等),仅适用于 /usr/bin/time。
  • -o FILE, –output=FILE
    将结果写入文件而非标准错误输出。
  • -a, –append
    与 -o 配合使用,追加内容到文件而非覆盖。
  • -f FORMAT, –format=FORMAT
    自定义输出格式(如 -f “Real: %E”)。
  • –quiet
    禁止显示错误信息(如命令不存在)。
  • -h, –help
    显示帮助信息。
  • -V, –version
    显示版本信息。

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
NAME

time - time a simple command or give resource usage

SYNOPSIS

time [option ...] command [argument ...]

DESCRIPTION

The time command runs the specified program command with the given
arguments. When command finishes, time writes a message to
standard error 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)).

Note: some shells (e.g., bash(1)) have a built-in time command
that provides similar information on the usage of time and
possibly other resources. To access the real command, you may
need to specify its pathname (something like /usr/bin/time).

OPTIONS

-p When in the POSIX locale, use the precise traditional
format

"real %f\nuser %f\nsys %f\n"

(with numbers in seconds) where the number of decimals in
the output for %f is unspecified but is sufficient to
express the clock tick accuracy, and at least one.

EXIT STATUS

If command was invoked, the exit status is that of command.
Otherwise, it is 127 if command could not be found, 126 if it
could be found but could not be invoked, and some other nonzero
value (1–125) if something else went wrong.

ENVIRONMENT

The variables LANG, LC_ALL, LC_CTYPE, LC_MESSAGES, LC_NUMERIC, and
NLSPATH are used for the text and formatting of the output. PATH
is used to search for command.

GNU VERSION

Below a description of the GNU 1.7 version of time. Disregarding
the name of the utility, GNU makes it output lots of useful
information, not only about time used, but also on other resources
like memory, I/O and IPC calls (where available). The output is
formatted using a format string that can be specified using the -f
option or the TIME environment variable.

The default format string is:

%Uuser %Ssystem %Eelapsed %PCPU (%Xtext+%Ddata %Mmax)k
%Iinputs+%Ooutputs (%Fmajor+%Rminor)pagefaults %Wswaps

When the -p option is given, the (portable) output format is used:

real %e
user %U
sys %S

The format string
The format is interpreted in the usual printf(3)-like way.
Ordinary characters are directly copied, tab, newline, and
backslash are escaped using \t, \n, and \\, a percent sign is
represented by %%, and otherwise % indicates a conversion. The
program time will always add a trailing newline itself. The
conversions follow. All of those used by tcsh(1) are supported.

Time

%E Elapsed real time (in [hours:]minutes:seconds).

%e (Not in tcsh(1).) Elapsed real time (in seconds).

%S Total number of CPU-seconds that the process spent in
kernel mode.

%U Total number of CPU-seconds that the process spent in user
mode.

%P Percentage of the CPU that this job got, computed as (%U +
%S) / %E.

Memory

%M Maximum resident set size of the process during its
lifetime, in Kbytes.

%t (Not in tcsh(1).) Average resident set size of the
process, in Kbytes.

%K Average total (data+stack+text) memory use of the process,
in Kbytes.

%D Average size of the process's unshared data area, in
Kbytes.

%p (Not in tcsh(1).) Average size of the process's unshared
stack space, in Kbytes.

%X Average size of the process's shared text space, in Kbytes.

%Z (Not in tcsh(1).) System's page size, in bytes. This is a
per-system constant, but varies between systems.

%F Number of major page faults that occurred while the process
was running. These are faults where the page has to be
read in from disk.

%R Number of minor, or recoverable, page faults. These are
faults for pages that are not valid but which have not yet
been claimed by other virtual pages. Thus the data in the
page is still valid but the system tables must be updated.

%W Number of times the process was swapped out of main memory.

%c Number of times the process was context-switched
involuntarily (because the time slice expired).

%w Number of waits: times that the program was context-
switched voluntarily, for instance while waiting for an I/O
operation to complete.

I/O

%I Number of filesystem inputs by the process.

%O Number of filesystem outputs by the process.

%r Number of socket messages received by the process.

%s Number of socket messages sent by the process.

%k Number of signals delivered to the process.

%C (Not in tcsh(1).) Name and command-line arguments of the
command being timed.

%x (Not in tcsh(1).) Exit status of the command.

GNU options
-f format, --format=format
Specify output format, possibly overriding the format
specified in the environment variable TIME.

-p, --portability
Use the portable output format.

-o file, --output=file
Do not send the results to stderr, but overwrite the
specified file.

-a, --append
(Used together with -o.) Do not overwrite but append.

-v, --verbose
Give very verbose output about all the program knows about.

-q, --quiet
Don't report abnormal program termination (where command is
terminated by a signal) or nonzero exit status.

GNU standard options
--help Print a usage message on standard output and exit
successfully.

-V, --version
Print version information on standard output, then exit
successfully.

-- Terminate option list.

BUGS

Not all resources are measured by all versions of UNIX, so some of
the values might be reported as zero. The present selection was
mostly inspired by the data provided by 4.2 or 4.3BSD.

GNU time version 1.7 is not yet localized. Thus, it does not
implement the POSIX requirements.

The environment variable TIME was badly chosen. It is not unusual
for systems like autoconf(1) or make(1) to use environment
variables with the name of a utility to override the utility to be
used. Uses like MORE or TIME for options to programs (instead of
program pathnames) tend to lead to difficulties.

It seems unfortunate that -o overwrites instead of appends. (That
is, the -a option should be the default.)

Mail suggestions and bug reports for GNU time to bug-time@gnu.org.
Please include the version of time, which you can get by running

time --version

and the operating system and C compiler you used.

SEE ALSO

bash(1), tcsh(1), times(2), wait3(2)

COLOPHON

This page is part of the man-pages (Linux kernel and C library
user-space interface documentation) project. Information about
the project can be found at
⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report
for this manual page, see
⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.
This page was obtained from the tarball man-pages-6.10.tar.gz
fetched from
⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on
2024-02-02. If you discover any rendering problems in this HTML
version of the page, or you believe there is a better or more up-
to-date source for the page, or you have corrections or
improvements to the information in this COLOPHON (which is not
part of the original manual page), send a mail to
man-pages@man7.org