performance - 在 zsh 中的每个命令之后自动执行内置时间(如 tcsh 中的 `set time`)

标签 performance time zsh

tcsh ,有一个非常有用的功能,可以自动打印每个命令的时间统计信息。可以通过非常简单的设置时间来启用它,以通过时间的名称设置“特殊shell变量”:

tcsh$ set time
tcsh$ sleep 1
0.000u 0.015s 0:01.01 0.9%      0+0k 0+0io 2241pf+0w
tcsh$ sleep 1 && sleep 0.1
0.000u 0.000s 0:01.01 0.0%      0+0k 0+0io 2241pf+0w
0.000u 0.015s 0:00.11 9.0%      0+0k 0+0io 2243pf+0w
tcsh$
tcsh$ man tcsh | fgrep -A2 'If set to a number,'
       time    If set to a number, then the time builtin (q.v.) executes auto‐
               matically after each command which takes more  than  that  many
               CPU seconds.  If there is a second word, it is used as a format
2.257u 0.319s 0:01.52 168.4%    0+0k 0+0io 35517pf+0w
tcsh$ 

我尝试为 zsh 寻找类似的解决方案,但我在 zshoptions(1) 中找不到这样的选项或任何其他页面,我得到的只是更改 shell 底层功能的广泛配置设置(除了 EXTENDED_HISTORYINC_APPEND_HISTORY_TIME,它们虽然非常有用,但并不与我正在寻找的相同)。

zsh没有类似的选项吗?有没有办法打印一组命令中每个命令的执行时间?

最佳答案

该选项实际上位于 zshparam 中,不是zshoptions ,称为REPORTTIME

zsh% REPORTTIME=
% REPORTTIME=
zsh% sleep 1
sleep 1  0.02s user 0.01s system 2% cpu 1.039 total
zsh% sleep 1 && sleep 0.1
sleep 1  0.03s user 0.01s system 3% cpu 1.094 total
sleep 0.1  0.01s user 0.01s system 17% cpu 0.130 total
zsh%
% man zshparam | fgrep -B1 -A3 'If nonnegative'
       REPORTMEMORY
              If nonnegative, commands whose maximum resident set size
              (roughly speaking, main memory usage) in kilobytes is greater
              than this value have timing statistics reported.  The format
              used to output statistics is the value of the TIMEFMT parameter,
--
       REPORTTIME
              If nonnegative, commands whose combined user and system
              execution times (measured in seconds) are greater than this
              value have timing statistics printed for them.  Output is
              suppressed for commands executed within the line editor,
man zshparam  0.06s user 0.01s system 94% cpu 0.071 total
fgrep -B1 -A3 'If nonnegative'  0.02s user 0.02s system 67% cpu 0.069 total
%

关于performance - 在 zsh 中的每个命令之后自动执行内置时间(如 tcsh 中的 `set time`),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69338321/

相关文章:

C++ 循环重复代码

bash - bash 时间、set -e 和 false 的奇怪交互,bash 3 与 bash 4 的差异

java - 使用 SimpleDateFormat 在 Java 中无法解析时间

bash - 在 Bash 中,每一段 "${BASH_SOURCE:-${(%):-%x}}"的含义是什么

mysql - 使用许多子选择优化 MySQL 查询

c# - 如何对 MySQL 查询进行排队,使它们顺序进行而不是并发,并防止过度使用 I/O?

javascript - 为什么 $(div#id) 比 $(#id) 慢?

java - (增量时间)在 Java 中每秒获取 60 个更新

Unix: (pwd now: ~) 来自 cd 命令?

macos - 获取包含要执行的管道的 zsh 别名