在 Mac OS 上测量 shell 脚本执行时间(以毫秒为单位)

use*_*382 11 macos shell time milliseconds

我想知道是否有办法从 Mac OS 上的 shell 脚本获取以毫秒为单位的时间。

我需要它来计算某个查询运行的时间。

现在我只能得到以秒为单位的时间:

Start=`date +%s`
End =`date +%s`
Time=$Start-$End
Run Code Online (Sandbox Code Playgroud)

Die*_*ich 17

您可以使用基准测试工具hyperfinehttps://github.com/sharkdp/hyperfine)。

\n

它比 更复杂time,默认情况下多次运行命令并给出平均运行时间、偏差、最小值和最大值。

\n

使用简单

\n
hyperfine your_command\n
Run Code Online (Sandbox Code Playgroud)\n

结果如下所示(hyperfine \'sleep 0.5\' 的结果):

\n
bash-3.2$ hyperfine \'sleep 0.5\'\nBenchmark #1: sleep 0.5\n  Time (mean \xc2\xb1 \xcf\x83):     505.6 ms \xc2\xb1   1.5 ms    [User: 0.8 ms, System: 1.2 ms]\n  Range (min \xe2\x80\xa6 max):   503.1 ms \xe2\x80\xa6 508.8 ms    10 runs\n
Run Code Online (Sandbox Code Playgroud)\n

有一点需要注意,最小运行次数是 2 ( hyperfine -r 2 \'your command\')。

\n

安装

\n

Hyperfine 可以通过 Homebrew 安装:

\n
brew install hyperfine\n
Run Code Online (Sandbox Code Playgroud)\n

有关更多信息,请参阅:https://github.com/sharkdp/hyperfine#on-macos

\n


Oli*_*lac 9

只需使用“时间”命令:

time something
Run Code Online (Sandbox Code Playgroud)

可能是 shell 或命令(查找等)

“真实”时间是您想要的总经过时间,包括毫秒