EdvardM
9/26/2017 - 1:22 PM

Script to run command multiple times and return averages

Script to run command multiple times and return averages

#!/bin/bash

# Script originally by bobmcn, taken from 
# https://stackoverflow.com/questions/17601539/calculate-the-average-of-several-time-commands-in-linux

# Changelog: added NSAMPLES to run given script only given number of times (defaults to 3)

NSAMPLES=${NSAMPLES-3}
rm -f /tmp/mtime.$$

for x in `seq 1 $NSAMPLES`
do
  /usr/local/bin/gtime -f "real %e user %U sys %S" -a -o /tmp/mtime.$$ $@
  tail -1 /tmp/mtime.$$
done

echo '---------------------------------------------'

awk '{ et += $2; ut += $4; st += $6; count++ } END {  printf "Average:\nreal %.3f user %.3f sys %.3f\n", et/count, ut/count, st/count }' /tmp/mtime.$$