平均运行时间在若干运行的 [英] Mean running time over a number of runs
问题描述
我想使用 time命令一报告的平均运行时间(计算它运行后的脚本>),也就是说,的 N 的时间。有一个简单的庆典
命令,脚本或系统实用程序,我不知道,这会允许我这样做?
I would like to report the mean running time (computed using the time command) of a script after running it, say, n times. Is there a simple bash
command, script, or system utility that I'm unaware of and that would allow me to do that?
推荐答案
这是一个有趣的问题<一href=\"http://blog.hozbox.com/2011/11/22/average-time-for-running-a-command-plus-20-other-averages/\">so我花了几分钟,并写了一个脚本,它的。下面是的光的这需要在具有运行时间
命令的输出文件的脚本版本。 <一href=\"http://blog.hozbox.com/2011/11/22/average-time-for-running-a-command-plus-20-other-averages/\">The 有更多的选择,并采取在命令完整剧本,直接在直接运行的平均水平。
This is an interesting question so I took a few minute and wrote a script for it. Below is the light version of the script which takes in a file that has the output of running the time
command. The full script has many more options and takes in a command directly to run averages on directly.
code:
#!/bin/bash
# calculate the mean average of wall clock time from multiple /usr/bin/time results.
file=${1}
cnt=0
if [ ${#file} -lt 1 ]; then
echo "you must specify a file containing output of /usr/bin/time results"
exit 1
elif [ ${#file} -gt 1 ]; then
samples=(`grep --color=never real ${file} | awk '{print $2}' | cut -dm -f2 | cut -ds -f1`)
for sample in `grep --color=never real ${file} | awk '{print $2}' | cut -dm -f2 | cut -ds -f1`; do
cnt=$(echo ${cnt}+${sample} | bc -l)
done
# Calculate the 'Mean' average (sum / samples).
mean_avg=$(echo ${cnt}/${#samples[@]} | bc -l)
mean_avg=$(echo ${mean_avg} | cut -b1-6)
printf "\tSamples:\t%s \n\tMean Avg:\t%s\n\n" ${#samples[@]} ${mean_avg}
fi
示例(在这里我命名为剧本 timeit.sh
和搭配chmod 775 timeit.sh
和 CHOWN乔恩timeit.sh
*):
[ 09:22 jon@hozbox.com ~ ]$ /usr/bin/time -a -o times.log -p sleep 3
[ 09:23 jon@hozbox.com ~ ]$ /usr/bin/time -a -o times.log -p sleep 1
[ 09:23 jon@hozbox.com ~ ]$ /usr/bin/time -a -o times.log -p sleep 2
[ 09:23 jon@hozbox.com ~ ]$ /usr/bin/time -a -o times.log -p sleep 2
[ 09:23 jon@hozbox.com ~ ]$ /usr/bin/time -a -o times.log -p sleep 7
[ 09:23 jon@hozbox.com ~ ]$ /usr/bin/time -a -o times.log -p sleep 0.5
[ 09:23 jon@hozbox.com ~ ]$ ./timeit.sh times.log
Samples: 6
Mean Avg: 2.5833
[ 09:23 jon@hozbox.com ~ ]$ cat times.log
real 3.00
user 0.00
sys 0.00
real 1.00
user 0.00
sys 0.00
real 2.00
user 0.00
sys 0.00
real 2.00
user 0.00
sys 0.00
real 7.00
user 0.00
sys 0.00
real 0.50
user 0.00
sys 0.00
* CHOWN
必要心不是在这里,但我不能帮助它! :)
*chown
isnt necessary here, but I couldn't help it! :)
人时间
:
语法结果
时间[选项...]命令[参数...]
选项
-o FILE --output = FILE
结果
写的资源利用统计数据文件。
-o FILE --output=FILE
Write the resource use statistics to FILE.
-a --append
结果
的追加的资源使用信息的输出文件,而不是
的覆盖它。
-a --append
Append the resource use information to the output file instead
of overwriting it.
-o FILE --output = FILE
结果
写的资源利用统计数据文件。默认情况下,
的覆盖的文件,摧毁该文件的previous内容。
-o FILE --output=FILE
Write the resource use statistics to FILE. By default, this
overwrites the file, destroying the file's previous contents.
-p --portability
结果
使用POSIX格式。
-p --portability
Use the POSIX format.
这篇关于平均运行时间在若干运行的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!