Created
January 27, 2016 23:48
-
-
Save Treferwynd/b32515f9d5ca6c564335 to your computer and use it in GitHub Desktop.
Bash function to measure basic stats of a command's running times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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