我想在一些不同的条件下在linux shell中执行一些东西,并且能够输出每次执行的执行时间。

我知道我可以写一个perl或python脚本,可以做到这一点,但有一种方法我可以在shell中做到这一点吗?(恰好是bash)


当前回答

您可以获得比bash内置时间(即time(1), Robert Gamble提到过)更详细的信息。通常这是/usr/bin/时间

编者按: 为了确保您调用的是外部实用程序time而不是shell的time关键字,请以/usr/bin/time来调用它 time是posix强制的实用程序,但它需要支持的唯一选项是-p。 特定的平台实现特定的、非标准的扩展:-v与GNU的time实用程序一起工作,如下所示(问题被标记为linux);BSD/macOS实现使用-l来产生类似的输出-参见man 1次。

详细输出示例:

$ /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: 1%
       Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.05
       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): 0
       Average resident set size (kbytes): 0
       Major (requiring I/O) page faults: 0
       Minor (reclaiming a frame) page faults: 210
       Voluntary context switches: 2
       Involuntary context switches: 1
       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的-f选项来输出节省时间的代码。下面是我最近使用的一些代码,用于获取和排序整个班级的学生程序的执行时间:

fmt="run { date = '$(date)', user = '$who', test = '$test', host = '$(hostname)', times = { user = %U, system = %S, elapsed = %e } }"
/usr/bin/time -f "$fmt" -o $timefile command args...

后来我将所有$timefile文件连接起来,并将输出输出到Lua解释器中。你可以用Python或bash或任何你喜欢的语法做同样的事情。我喜欢这个技巧。

您可以获得比bash内置时间(即time(1), Robert Gamble提到过)更详细的信息。通常这是/usr/bin/时间

编者按: 为了确保您调用的是外部实用程序time而不是shell的time关键字,请以/usr/bin/time来调用它 time是posix强制的实用程序,但它需要支持的唯一选项是-p。 特定的平台实现特定的、非标准的扩展:-v与GNU的time实用程序一起工作,如下所示(问题被标记为linux);BSD/macOS实现使用-l来产生类似的输出-参见man 1次。

详细输出示例:

$ /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: 1%
       Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.05
       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): 0
       Average resident set size (kbytes): 0
       Major (requiring I/O) page faults: 0
       Minor (reclaiming a frame) page faults: 210
       Voluntary context switches: 2
       Involuntary context switches: 1
       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和subshell ():

time (
  for (( i=1; i<10000; i++ )); do
    echo 1 >/dev/null
  done
)

或者在同一个shell{}中:

time {
  for (( i=1; i<10000; i++ )); do
    echo 1 >/dev/null
  done
}

一个可能简单的方法(可能不能满足不同用户的需求)是使用shell PROMPT。这是一个简单的解决方案,在某些情况下可能有用。你可以使用bash提示功能,如下例所示:

export PS1='[\t \u@\h]\$' 

上面的命令将导致shell提示符更改为:

[HH:MM:SS username@hostname]$ 

每次运行命令(或按回车键)返回到shell提示符时,提示符将显示当前时间。

notes: 1) beware that if you waited for sometime before you type your next command, then this time need to be considered, i.e the time displayed in the shell prompt is the timestamp when the shell prompt was displayed, not when you enter command. some users choose to hit Enter key to get a new prompt with a new timestamp before they are ready for the next command. 2) There are other available options and modifiers that can be used to change the bash prompt, refer to ( man bash ) for more details.

要逐行测量delta,请尝试用指针测量。

$ npm install -g gnomon
$ <your command> | gnomon --medium=1.0 --high=4.0 --ignore-blank --real-time=100

一个命令行实用程序,有点像moreutils的ts,用于将时间戳信息预先添加到另一个命令的标准输出中。对于长时间运行的流程非常有用,因为您需要记录花费如此长时间的事情。

还可以使用——high和/或——medium选项指定以秒为单位的长度阈值,超过该阈值,gnomon将以红色或黄色突出显示时间戳。你还可以做一些其他的事情。