time命令

-- TOC --

time命令主要用来观察某个程序的执行时间,使用此命令很简单,理解其输出稍微费点劲。

显示系统时间,使用date命令。

builtin time

bash builin了一个time命令,一般用这个就可以:

$ time python3 pingscan.py 192.168.16.1/24
Pingscan network 192.168.16.1/24 with 128 workers (mt_ping):
192.168.16.1       4/4
192.168.16.100     4/4
Hosts: 2/254

real    0m5.554s
user    0m0.623s
sys 0m0.731s

网络中只有我的主机和网管。

time命令的输出有3个时间:

  1. real,表示wall clock,实际经过的时间;
  2. user,表示user cpu time,进程用户态时间;
  3. sys,表示system cpu time,进程内核态时间;

在uniprocessor环境下,基本上 real >= user + sys

在multiprocessor环境下就不一定了,当程序可以充分利用多核的时候,很可能得到的结果是 real < user + sys

/usr/bin/time

这是另一个time程序,它的输出要稍微复杂一点:

$ \time python3 pingscan.py 192.168.16.1/24
Pingscan network 192.168.16.1/24 with 128 workers (mt_ping):
192.168.16.1       4/4
192.168.16.100     4/4
Hosts: 2/254
0.58user 0.80system 0:05.58elapsed 24%CPU (0avgtext+0avgdata 18288maxresident)k
0inputs+0outputs (0major+153517minor)pagefaults 0swaps

\escape的方式,阻止bash调用它内置的time程序。

一般要加上-v参数,结果看起来会方便一些:

$ \time -v python3 pingscan.py 192.222.0.0/20
Pingscan network 192.222.0.0/20 with 128 workers (mt_ping):
192.222.0.12       4/4
192.222.0.11       4/4
192.222.0.15       4/4
192.222.0.16       4/4
192.222.0.10       3/4
192.222.0.18       3/4
192.222.0.19       3/4
192.222.0.21       3/4
192.222.0.211      4/4
192.222.0.213      4/4
192.222.0.212      4/4
192.222.0.219      4/4
192.222.0.254      4/4
192.222.1.6        4/4
192.222.1.7        4/4
192.222.1.8        4/4
192.222.1.9        4/4
192.222.1.13       4/4
192.222.1.17       4/4
192.222.1.19       4/4
192.222.1.20       4/4
192.222.1.30       4/4
192.222.1.26       4/4
192.222.1.31       4/4
192.222.1.32       4/4
192.222.1.34       4/4
192.222.1.35       4/4
192.222.1.36       4/4
192.222.1.39       4/4
192.222.1.42       4/4
192.222.1.60       4/4
192.222.1.57       4/4
192.222.1.78       4/4
192.222.1.145      4/4
192.222.1.149      4/4
192.222.1.150      4/4
192.222.1.151      4/4
192.222.1.155      3/4
192.222.1.157      4/4
192.222.1.158      4/4
192.222.1.159      4/4
192.222.1.160      4/4
192.222.1.190      4/4
192.222.1.207      4/4
192.222.1.254      4/4
192.222.2.8        4/4
192.222.2.9        4/4
192.222.2.6        4/4
192.222.2.7        4/4
192.222.1.253      4/4
192.222.2.17       4/4
192.222.2.19       4/4
192.222.2.31       4/4
192.222.2.35       4/4
192.222.2.39       4/4
192.222.2.30       3/4
192.222.2.34       3/4
192.222.2.36       3/4
192.222.2.42       4/4
192.222.2.57       4/4
192.222.2.60       3/4
192.222.2.78       3/4
192.222.2.151      4/4
192.222.3.6        4/4
192.222.3.7        4/4
192.222.3.9        4/4
192.222.3.8        4/4
192.222.3.17       4/4
192.222.3.19       3/4
192.222.3.30       4/4
192.222.3.31       4/4
192.222.3.35       4/4
192.222.3.34       4/4
192.222.3.36       4/4
192.222.3.39       4/4
192.222.3.42       4/4
192.222.3.57       4/4
192.222.3.60       4/4
192.222.3.78       4/4
192.222.3.151      4/4
192.222.5.1        4/4
192.222.5.2        4/4
192.222.5.5        4/4
192.222.5.8        4/4
192.222.5.7        4/4
192.222.5.9        4/4
192.222.5.13       4/4
192.222.5.12       4/4
192.222.5.14       4/4
192.222.5.20       4/4
192.222.5.19       4/4
192.222.5.22       4/4
192.222.5.21       4/4
192.222.5.23       4/4
192.222.5.24       4/4
192.222.5.28       4/4
192.222.5.32       4/4
192.222.5.16       3/4
192.222.5.64       4/4
192.222.5.73       4/4
192.222.5.69       3/4
192.222.5.81       4/4
192.222.5.83       4/4
192.222.5.84       4/4
192.222.5.99       4/4
192.222.5.100      4/4
192.222.5.110      4/4
192.222.5.116      4/4
192.222.5.118      4/4
192.222.5.143      4/4
192.222.5.146      4/4
192.222.5.147      4/4
192.222.5.148      4/4
192.222.5.174      4/4
192.222.5.176      3/4
192.222.5.177      3/4
192.222.6.18       4/4
192.222.8.14       4/4
192.222.8.13       4/4
192.222.8.15       4/4
192.222.8.22       4/4
192.222.8.23       4/4
192.222.8.41       4/4
192.222.8.42       4/4
192.222.8.46       4/4
192.222.8.61       4/4
192.222.8.62       4/4
192.222.8.65       4/4
192.222.8.66       4/4
192.222.8.88       4/4
192.222.8.89       4/4
192.222.8.90       2/4
192.222.8.106      4/4
192.222.8.107      4/4
192.222.8.108      3/4
192.222.8.120      1/4
192.222.8.128      3/4
192.222.8.36       3/4
192.222.8.140      4/4
192.222.8.147      4/4
192.222.8.171      4/4
192.222.8.149      3/4
192.222.8.170      3/4
192.222.8.201      3/4
192.222.8.231      4/4
192.222.8.232      4/4
192.222.8.251      4/4
192.222.8.254      4/4
192.222.9.2        4/4
192.222.8.175      1/4
192.222.9.24       4/4
192.222.9.35       4/4
192.222.9.63       3/4
192.222.9.69       4/4
192.222.9.75       4/4
192.222.9.86       4/4
192.222.9.89       4/4
192.222.9.99       4/4
192.222.9.105      4/4
192.222.9.87       2/4
192.222.9.116      4/4
192.222.9.121      4/4
192.222.9.119      4/4
192.222.9.126      3/4
192.222.9.142      4/4
192.222.9.151      4/4
192.222.9.36       2/4
192.222.9.153      4/4
192.222.9.167      4/4
192.222.9.168      4/4
192.222.9.169      4/4
192.222.9.170      4/4
192.222.9.186      4/4
192.222.9.161      3/4
192.222.9.162      3/4
192.222.9.166      3/4
192.222.9.212      4/4
192.222.9.231      4/4
192.222.9.232      4/4
192.222.9.236      4/4
192.222.9.247      3/4
192.222.10.3       4/4
192.222.10.11      4/4
192.222.10.12      4/4
192.222.10.17      4/4
192.222.10.8       3/4
192.222.10.25      4/4
192.222.10.14      3/4
192.222.10.31      4/4
192.222.10.27      3/4
192.222.10.49      4/4
192.222.10.58      4/4
192.222.10.60      4/4
192.222.10.61      4/4
192.222.10.70      4/4
192.222.10.75      4/4
192.222.10.79      4/4
192.222.10.85      4/4
192.222.10.88      4/4
192.222.10.62      3/4
192.222.10.73      3/4
192.222.10.92      4/4
192.222.10.102     4/4
192.222.10.103     4/4
192.222.10.119     4/4
192.222.10.124     4/4
192.222.10.128     4/4
192.222.10.134     4/4
192.222.10.139     4/4
192.222.10.127     2/4
192.222.10.130     3/4
192.222.10.142     3/4
192.222.10.152     4/4
192.222.10.181     4/4
192.222.10.176     3/4
192.222.10.178     3/4
192.222.10.200     4/4
192.222.10.209     4/4
192.222.10.210     4/4
192.222.10.227     4/4
192.222.10.212     3/4
192.222.10.240     4/4
192.222.10.238     3/4
192.222.15.2       4/4
192.222.15.3       4/4
192.222.15.4       4/4
192.222.15.5       4/4
192.222.15.6       4/4
192.222.15.14      4/4
192.222.15.16      4/4
192.222.15.17      4/4
192.222.15.18      4/4
192.222.15.22      4/4
192.222.15.190     4/4
192.222.15.224     4/4
Hosts: 235/4094
    Command being timed: "python3 pingscan.py 192.222.0.0/20"
    User time (seconds): 9.15
    System time (seconds): 13.17
    Percent of CPU this job got: 27%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 1:21.88
    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): 23424
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 49
    Minor (reclaiming a frame) page faults: 2826958
    Voluntary context switches: 89032
    Involuntary context switches: 15236
    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

/usr/bin/time除了统计时间之外,还能够统计程序时间的内存情况,CPU占用等信息,只是这些统计信息还有些看不懂。

本文链接:https://cs.pynote.net/sf/linux/shell/202201041/

-- EOF --

-- MORE --