time 命令不是用来看时间的,主要是用来统计程序执行的时间消耗(CPU 占用),但也可以统计内存、IO 等资源的使用情况。

查看 sleep 命令的用时情况:

time sleep 1
real    0m1.002s
user    0m0.002s
sys     0m0.000s

通常 time 只显示耗时信息,real 是实际用时(从开始执行到结束,包括了自己的 CPU 用时、被阻塞挂起耗时),user 是用户 CPU 用时(执行用户态程序),sys 是系统 CPU 用时(执行内核程序)。CPU 时间 = 用户CPU 时间 + 系统 CPU 时间,real 大于 CPU 时间是因为在执行过程中进程本身休眠、挂起,这些 CPU 时间其他进程在用。

想显示其他资源消耗使用参数 -v (--verbose),但可能提示不存在。原因是 time 可能不止一个。

type -a time
time is a shell keyword
time is /usr/bin/time
time is /bin/time

默认使用的是 shell 关键字(内建命令),不提供 -v

可以:

/usr/bin/time -v sleep 1
        Command being timed: "sleep 1"
        User time (seconds): 0.00
        System time (seconds): 0.00
        Percent of CPU this job got: 0%
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.00
        Average shared text size (kbytes): 0
        Average unshared data size (kbytes): 0
        Average stack size (kbytes): 0
        Average total size (kbytes): 0
        Maximum resident set size (kbytes): 2096
        Average resident set size (kbytes): 0
        Major (requiring I/O) page faults: 0
        Minor (reclaiming a frame) page faults: 75
        Voluntary context switches: 2
        Involuntary context switches: 0
        Swaps: 0
        File system inputs: 0
        File system outputs: 0
        Socket messages sent: 0
        Socket messages received: 0
        Signals delivered: 0
        Page size (bytes): 4096
        Exit status: 0

或者:

\time"time"'time'command time

这样就不使用内建命令了。