pr227-measure.sh (858B)
1 #!/usr/bin/env bash 2 3 if [[ ${EPOCHREALTIME-} ]]; then 4 measure() { 5 local beg=$EPOCHREALTIME 6 eval "$1" 7 local end=$EPOCHREALTIME 8 echo "$(bc -l <<< "$end-$beg") $2" 9 } 10 11 else 12 cc -o epoch.tmp -x c - <<EOF 13 #include <sys/time.h> 14 #include <stdio.h> 15 int main() { 16 struct timeval tv; 17 gettimeofday(&tv, NULL); 18 printf("%ld.%06u\n", tv.tv_sec, tv.tv_usec); 19 } 20 EOF 21 22 chmod +x epoch.tmp 23 measure() { 24 local beg=$(./epoch.tmp) 25 eval "$1" 26 local end=$(./epoch.tmp) 27 echo "$(bc -l <<< "$end-$beg") $2" 28 } 29 30 trap 'rm -f epoch.tmp' EXIT 31 trap 'rm -f epoch.tmp; trap - INT; kill -INT $$' INT 32 fi 33 34 { 35 echo "# $BASH_VERSION ($MACHTYPE)" 36 for i in {0..100}; do 37 measure ":" 38 done 39 for i in {0..100}; do 40 measure "sleep 0.001" 0.001 41 done 42 for i in {2..300}; do 43 printf -v v '0.%03d' "$i" 44 measure "sleep $v" "$v" 45 done 46 }