Bash 测量执行时间并最终将其相加

Bash 测量执行时间并最终将其相加,bash,Bash,我目前正在编写一个bash测试框架,我想测量每个测试方法的执行时间。此外,我想把这些加起来,显示完整的执行时间 我知道您可以使用time来度量函数的执行时间,但我不知道如何存储单个结果并添加它们 时间真的是工作的正确工具吗?或者我应该使用类似于date 示例代码: declare -a execution_times # somehow measure execution time of test function # add the individual execution time ex

我目前正在编写一个bash测试框架,我想测量每个测试方法的执行时间。此外,我想把这些加起来,显示完整的执行时间

我知道您可以使用
time
来度量函数的执行时间,但我不知道如何存储单个结果并添加它们

时间真的是工作的正确工具吗?或者我应该使用类似于
date

示例代码:

declare -a execution_times

# somehow measure execution time of test function

# add the individual execution time
execution_times+=(execution_time)

for execution_time in "${execution_times[@]}"; do
  # compute full execution time
done

time
命令作为第一个输出提供您想要测量的
real
时间或walltime。

下面是一个测量sleep命令所用时间的示例:

rm times.txt
for i in $(seq 1 10) ; do
  /bin/time -f "%e" -o times.txt -a sleep .2
done

awk ' { t += $1 } END { print "Total:" t }' times.txt