Created
March 6, 2015 14:47
-
-
Save alexgeek/df334bfa03abd4cab977 to your computer and use it in GitHub Desktop.
Bash Execution Timer
This file contains hidden or 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
| #!/bin/bash | |
| die() { echo "$@" 1>&2 ; exit 1; } | |
| [ $# -gt 2 ] && die "Max 2 arguments." | |
| PROGRAM=$1 | |
| [ -z "$PROGRAM" ] && die "No arguments passed." | |
| [ ! -f "$PROGRAM" ] && die "Program does not exist." | |
| ITER=${2:-1000} # default 1000 iterations | |
| echo "Executing $ITER tests on $PROGRAM." | |
| for i in `seq 1 $ITER`; do | |
| (time ./$PROGRAM) 2>&1 > /dev/null | grep real | awk '{print $2}'|sed "s/^[0-9]\+m//" | |
| done | awk '{sum += $1} END {print sum / NR, "s"}' |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
About
This script displays the average execution time of the given program.
By default the program will be run 1000 times unless a second parameter is given.
It checks for the existence of the file first as otherwise
timewill return the time taken to execute a non-existent program and trick you into thinking your program is lightning fast.Usage
./test.sh program [iterations]Notes
On line 12 we redirect the output of the time command then strip away most of
time's output.