Skip to content

Instantly share code, notes, and snippets.

@Treferwynd
Created January 27, 2016 23:48
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 Treferwynd/b32515f9d5ca6c564335 to your computer and use it in GitHub Desktop.
Save Treferwynd/b32515f9d5ca6c564335 to your computer and use it in GitHub Desktop.
Bash function to measure basic stats of a command's running times
# Run a function multiple times and output the average time
runtime() {
quiet=""
if [ $1 = "-q" ]; then
quiet="quiet"
shift
fi
number=$1
shift
tot=0
max=-1
min=10000
for i in `seq $number`; do
tmp=$({ time $@ > /dev/null; } 2>&1 | perl -ne 'print if s/(real\s+0m)([\.\d]*)(s)/\2/;')
tot=$(python -c "print($tot + $tmp)")
max=$(python -c "print(max($max, $tmp))")
min=$(python -c "print(min($min, $tmp))")
if [ -z "$quiet" ]; then
python -c "print('{:4} / {:<4}: {} s'.format($i, $number, $tmp))";
fi
done
python -c "print('Average: {:7.3f} s'.format($tot/$number))"
python -c "print('Max: {:7.3f} s'.format($max))"
python -c "print('Min: {:7.3f} s'.format($min))"
python -c "print('Total: {:7.3f} s'.format($tot))"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment