我想知道是否有办法从Mac OS上的shell脚本获取时间(毫秒)。
我需要它来计算某些查询运行的时间。
现在我只能在几秒钟内得到时间:
Start=`date +%s`
End =`date +%s`
Time=$Start-$End
答案 0 :(得分:3)
只需使用' time'命令:
time something
某些东西可以是shell,也可以是命令(find等)
"真实" time是您想要的总耗用时间,包括毫秒答案 1 :(得分:1)
您可以使用基准测试工具 hyperfine
(https://github.com/sharkdp/hyperfine)。
它比 time
更复杂,默认情况下会多次运行您的命令,并为您提供平均运行时间、偏差、最小值和最大值。
简单使用
hyperfine your_command
结果如下所示 (result of hyperfine 'sleep 0.5'):
bash-3.2$ hyperfine 'sleep 0.5'
Benchmark #1: sleep 0.5
Time (mean ± σ): 505.6 ms ± 1.5 ms [User: 0.8 ms, System: 1.2 ms]
Range (min … max): 503.1 ms … 508.8 ms 10 runs
有一个警告,最小运行次数为 2 (hyperfine -r 2 'your command'
)。
安装
Hyperfine 可以通过 Homebrew 安装:
brew install hyperfine
答案 2 :(得分:0)
由于Mac OS是类似BSD的系统,因此date
不支持您需要的%N参数。
您可以考虑安装GNU Core Utils。
它允许您在the usual Linux way中获得时间。
我想,time something
命令也会以毫秒为单位输出结果。