Skip to content

Instantly share code, notes, and snippets.

@vitalymak
Last active May 18, 2017 11:16
Show Gist options
  • Save vitalymak/87c6051e7097b7774bf971f4a2834dc0 to your computer and use it in GitHub Desktop.
Save vitalymak/87c6051e7097b7774bf971f4a2834dc0 to your computer and use it in GitHub Desktop.
macOS get average time for 10 command runs, using coreutils/date and node
atime() {
local times=()
local start end total
for i in {1..10}; do
start=$(/usr/local/opt/coreutils/libexec/gnubin/date +%s%N)
"$@" >/dev/null 2>&1
end=$(/usr/local/opt/coreutils/libexec/gnubin/date +%s%N)
total=$(expr $end - $start)
times+=( $total )
node -p "Math.round(($total)/1000)/1000000"
done
times=$(echo ${times[@]} | tr " " ",")
node -e " \
a=[${times}]; \
r=i=>Math.round(i/1000)/1000000; \
a.sort((a,b)=>a-b); \
avg=a.reduce((a,b)=>a+b)/a.length; \
precision=Math.round((avg-a[0])/avg*100); \
console.log('------------');
console.log(r(avg) + ' ±' + precision + '%'); \
"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment