Skip to content

Instantly share code, notes, and snippets.

@edvardm
Created September 26, 2017 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edvardm/979712deedbbb7d23a88637727220b52 to your computer and use it in GitHub Desktop.
Save edvardm/979712deedbbb7d23a88637727220b52 to your computer and use it in GitHub Desktop.
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.$$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment