Bash 多次运行的平均运行时间

Bash 多次运行的平均运行时间,bash,Bash,我想报告脚本运行n次后的平均运行时间(使用计算)。是否有一个简单的bash命令、脚本或系统实用程序是我不知道的,并且允许我这样做?没有一个命令可以做到这一点。您可以做的是将每次运行的时间附加到一个文件中,然后根据这些数字计算平均值。这是一个有趣的问题。下面是脚本的轻版本,它接收一个文件,该文件具有运行time命令的输出。具有更多选项,并直接接受命令以直接运行平均值 代码: 示例(这里我将脚本命名为timeit.sh和chmod 775 timeit.sh和chown jon timeit.sh*

我想报告脚本运行n次后的平均运行时间(使用计算)。是否有一个简单的
bash
命令、脚本或系统实用程序是我不知道的,并且允许我这样做?

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

这是一个有趣的问题。下面是脚本的轻版本,它接收一个文件,该文件具有运行
time
命令的输出。具有更多选项,并直接接受命令以直接运行平均值

代码:

示例(这里我将脚本命名为
timeit.sh
chmod 775 timeit.sh
chown jon timeit.sh
*):


*
chown
在这里不是必需的,但我没办法!)

:

语法
time[option…]命令[arg…]

选择权

-o FILE--output=FILE

将资源使用统计信息写入文件

-a--追加

将资源使用信息附加到输出文件 覆盖它的方法

-o FILE--output=FILE

将资源使用统计信息写入文件。默认情况下,此 覆盖文件,销毁文件以前的内容

-p--可移植性

使用POSIX格式


我在这里问了一些类似的问题:。建议使用一个Perl脚本,它可以运行软件几次并打印出一些统计数据。

我真的很喜欢这个。
#!/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 
[ 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