bash - 如何获得命令运行所需的总时间?

标签 bash shell

我知道您可以使用如下方法获取每个函数的编译时间:

xcodebuild -workspace App.xcworkspace -scheme App clean build OTHER_SWIFT_FLAGS="-Xfrontend -debug-time-function-bodies" | grep .[0-9]ms | grep -v ^0.[0-9]ms | sort -nr > culprits.txt

或者这个

defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES

但是我怎样才能在命令行中打印出总构建时间呢?

最佳答案

一种稍微更有效的方法 - 这里我们只调用 date 两次:

#!/bin/bash
start_time=$(date +%s)
# xcode command here
end_time=$(date +%s)
printf 'Elapsed time is %d seconds\n' $((end_time - start_time))

即使没有 fork ,也可以通过 printf 获取当前时间:

#!/bin/bash
printf -v start_time '%(%s)T' -1
# xcode command here
printf -v end_time   '%(%s)T' -1
printf 'Elapsed time is %d seconds\n' $((end_time - start_time))

关于bash - 如何获得命令运行所需的总时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43149568/

相关文章:

shell - 意外地用 sed 替换 &

bash - 如何在 shell 脚本中优雅地处理内存不足

linux - 在 linux shell 中连接别名

git - 如何从git ls-remote捕获错误代码?

bash - 如何在 bash 参数中保留引用的字符串

linux - 在远程 linux 后台运行 Xvfb

linux - Linux 中的精确 grep -f 命令

c - 如何在 OSX 上找到 Clang 文件夹?

mysql - 无法连接到 mysql docker 容器

bash - Linux屏幕命令自动化