Last active
September 11, 2018 15:10
-
-
Save tomislacker/8ada554ba265f022c8a7 to your computer and use it in GitHub Desktop.
Shell Get 'microtime(TRUE)'
This file contains 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 | |
DEFAULT_SCALE=4 | |
getMicrotime () | |
{ | |
date +%s%N | |
} | |
microElapse () | |
{ | |
local microTimeA=$1 | |
local microTimeB=$2 | |
local scale=${3:-${DEFAULT_SCALE}} | |
echo "scale=${scale}; ${microTimeB}-${microTimeA}" | bc -l | |
} | |
microElapseToSecs () | |
{ | |
local microTimeA=$1 | |
local microTimeB=$2 | |
local scale=${3:-${DEFAULT_SCALE}} | |
echo "scale=${scale}; $(microElapse ${microTimeA} ${microTimeB} ${scale})/1000000000" | bc -l | |
} | |
echo -e "\t###" | |
echo -e "\t# Example" | |
echo -e "\t###" | |
a=$(getMicrotime) | |
echo "a: '${a}'" | |
sleep 1 | |
b=$(getMicrotime) | |
echo "b '${b}'" | |
elapsed=$(microElapseToSecs ${a} ${b}) | |
echo "========================" | |
echo -e "elapsed(u):\t'$(microElapse ${a} ${b})'" | |
echo -e "elapsed(s):\t'$(microElapseToSecs ${a} ${b})'" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment