找回密码
 立即注册
搜索
查看: 295|回复: 0

Linux time 不是用来看时间的

[复制链接]

266

主题

0

回帖

1119

积分

管理员

积分
1119
发表于 2024-1-15 14:46:35 | 显示全部楼层 |阅读模式

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

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


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|棱讯科技 ( 粤ICP备2024228160号-2|粤公网安备44030002003510号 )

GMT+8, 2024-7-27 12:45 , Processed in 0.015331 second(s), 3 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表