Skip to content

Instantly share code, notes, and snippets.

@alexgeek
Created March 6, 2015 14:47
Show Gist options
  • Select an option

  • Save alexgeek/df334bfa03abd4cab977 to your computer and use it in GitHub Desktop.

Select an option

Save alexgeek/df334bfa03abd4cab977 to your computer and use it in GitHub Desktop.
Bash Execution Timer
#!/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"}'
@alexgeek
Copy link
Author

alexgeek commented Mar 6, 2015

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 time will 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment