Skip to content

Instantly share code, notes, and snippets.

@tomislacker
Last active September 11, 2018 15:10
Show Gist options
  • Save tomislacker/8ada554ba265f022c8a7 to your computer and use it in GitHub Desktop.
Save tomislacker/8ada554ba265f022c8a7 to your computer and use it in GitHub Desktop.
Shell Get 'microtime(TRUE)'
#!/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