注意:Linux中的time有两个,一个是关键字time,另一个是/usr/bin/time
1 time
该命令只能统计运行时间
Usage: time command [args…]
Example: time ls -l 命令结果在stdout,时间结果在stderr
PS: 如果想把时间结果重定向,需要以下操作{ time ls -l; } 2> out
注意 {
后面要有空格,命令后面要有 ;
或者用 (time ls -l) 2> out
不需要空格和 ;
2 /usr/bin/time
该命令功能更多,可以统计时间,CPU,内存等
Usage: /usr/bin/time [-apvV] [-f format] [-o file] [--append] [--verbose]
[--portability] [--format=format] [--output=file] [--version]
[--help] command [arg...]
基本用法 /usr/bin/time ls -l
显示便携结果 /usr/bin/time -p ls -l
类似 time
显示详细结果 /usr/bin/time -v ls -l
自定义输出格式 usr/bin/time -f '\nElapsed:\t%Es\nCPU:\t%P\nMemory:\t%Mkb\n\nExit:\t%x' ls -l /* 1> /dev/null
format string支持\t, \n, \, %%, 详请如下
- 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 com‐mand being timed.
%x (Not in tcsh(1).) Exit status of the command.