Average run time for multiple runs

I would like to report the average runtime (calculated using the time command ) of the script after running, say, n times. Is there a simple command bash, script, or system utility that I don’t know about, and that will allow me to do this?

+4
source share
3 answers

This is an interesting question, so I took a few minutes and wrote a script for it . Below is a lightweight version of a script that takes to a file that outputs a command time. There are many other options in the full script and a command is required directly to run the averages.

the 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 

( script timeit.sh chmod 775 timeit.sh chown jon 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 , !:)

man time:


time [option...] command [arg...]

-o FILE --output=FILE
  FILE.

-a --append
     .

-o FILE --output=FILE
  FILE.   , .

-p --portability
  POSIX.

+11

- : . Dumbbench, Perl script, .

+1

, . , .

0

All Articles