2011-11-21 52 views
4

的运行时间,我想运行它,比如说后报告的平均运行时间脚本(使用time command计算),ñ倍。有没有一个简单的bash命令,脚本或系统实用程序,我不知道,这将允许我这样做?平均在多个运行

+1

http://superuser.com/questions/603897/how-to-measure-average-execution-time-of-a-script –

回答

0

没有一个单一的命令可以做到这一点。你可以做的是将每次运行的时间附加到一个文件中,然后计算这些数字的平均值。

11

这是一个有趣的问题so I took a few minute and wrote a script for it。以下是指示灯脚本的版本,该脚本输入的文件具有运行time命令的输出。 The full script有更多的选项,并直接接受命令直接运行平均值。

代码:

#!/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.shchmod 775 timeit.shchown jon timeit.sh *):

[ 09:22 [email protected] ~ ]$ /usr/bin/time -a -o times.log -p sleep 3 
[ 09:23 [email protected] ~ ]$ /usr/bin/time -a -o times.log -p sleep 1 
[ 09:23 [email protected] ~ ]$ /usr/bin/time -a -o times.log -p sleep 2 
[ 09:23 [email protected] ~ ]$ /usr/bin/time -a -o times.log -p sleep 2 
[ 09:23 [email protected] ~ ]$ /usr/bin/time -a -o times.log -p sleep 7 
[ 09:23 [email protected] ~ ]$ /usr/bin/time -a -o times.log -p sleep 0.5 
[ 09:23 [email protected] ~ ]$ ./timeit.sh times.log 
     Samples:  6 
     Mean Avg:  2.5833 

[ 09:23 [email protected]zbox.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心不是在这里有必要,但我忍不住它! :)

man time

语法
time [option...] command [arg...]

选项

-o FILE --output=FILE
写的资源使用统计文件。

-a --append
追加资源使用信息的输出文件覆盖它,而不是 。

-o FILE --output=FILE
将资源使用统计信息写入FILE。默认情况下,这个 覆盖该文件,销毁该文件的以前的内容。

-p --portability
使用POSIX格式。

+0

我真的很喜欢这个。 – mathiasrw

1

我在这里问的是类似的东西:Average Execution Time。建议使用Dumbbench,这是一个Perl脚本,它可以多次运行该软件并打印出一些统计数据。