Timer: Difference between revisions
| Line 29: | Line 29: | ||
# expected result: | # expected result: | ||
# Elapsed time 00h:00m:02s | # Elapsed time 00h:00m:02s | ||
</pre> | |||
This following script measures elapsed time with millisecond precision | |||
<pre> | |||
# start the timer | |||
START=$(date +%s.%N) | |||
# sleep 2 seconds for testing purpose | |||
sleep 2s | |||
# stop the timer | |||
END=$(date +%s.%N) | |||
DIFF=$(awk "BEGIN {printf \"%.3f\", $END - $START}") | |||
# Use awk to break the seconds down into h/m/s while preserving the decimal part | |||
printf "Elapsed time %s\n" "$(awk -v t="$DIFF" 'BEGIN { | |||
h = int(t/3600) | |||
m = int((t%3600)/60) | |||
s = t%60 | |||
printf "%02dh:%02dm:%06.3fs", h, m, s | |||
}')" | |||
# expected result: | |||
# Elapsed time 00h:00m:02.002s | |||
</pre> | </pre> | ||