Skip to content

Instantly share code, notes, and snippets.

@swarminglogic
Last active December 28, 2017 11:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save swarminglogic/87adb0bd0850d76ba09f to your computer and use it in GitHub Desktop.
Save swarminglogic/87adb0bd0850d76ba09f to your computer and use it in GitHub Desktop.
Millisecond resolution tic / toc timer pair utility for linux terminal. Supports id-based tic/toc pairs.
#!/bin/bash
function showHelp {
version=0.0.1
versionDate="2014-07-07"
echo "$0 - tic/toc timer pair
Usage: $0 [id] Stores initial time (w/optional id marker)
$0 [-u|--unique] Creates and returns unique id
Using optional ID is recommended, as it allows simulatenous usage.
Notes:
ttoc [id] (displays delta time since ttic was called)
Temporary timer file is stored at /tmp/.ttic.[ID.]time
Example
# Global timer (not recommended)
ttic && (do work) && ttoc
# Using FooBar as id
ttic FooBar && (do work) && ttoc FooBar
# Using a randomly generated id
id=\$(ttic -u) && (do work) && ttoc \$id
Mainted at: https://gist.github.com/swarminglogic/87adb0bd0850d76ba09f
Author: Roald Fernandez (github@swarminglogic.com)
Version: $version ($versionDate)
License: CC-zero (public domain)
"
exit $1
}
while test $# -gt 0; do
case "$1" in
-h|--help)
showHelp 0
;;
-u|--unique)
shift
hasId=yes
isGenerated=yes
id=$(tr -dc "[:alpha:]" < /dev/urandom | head -c 8)
;;
*)
hasId=yes
id=$1
shift
break
;;
esac
done
if [[ $hasId ]] ; then
tmpfile=/tmp/.ttic.${id}.time
if [[ $isGenerated ]] ; then
echo $id
fi
else
tmpfile=/tmp/.ttic.time
fi
echo $(($(date +%s%N)/1000000)) > $tmpfile
#!/bin/bash
function showHelp {
version=0.0.1
versionDate="2014-07-07"
echo "$0 - tic/toc timer pair
Usage: $0 [id]
Stores initial time (w/optional id marker)
Notes:
ttic [id] (used for marking initial time)
Temporary timer file is stored at /tmp/.ttic.[ID.]time
Example
# Global timer (not recommended)
ttic && (do work) && ttoc
# Using FooBar as id
ttic FooBar && (do work) && ttoc FooBar
# Using a randomly generated id
id=\$(ttic -u) && (do work) && ttoc \$id
Mainted at: https://gist.github.com/swarminglogic/87adb0bd0850d76ba09f
Author: Roald Fernandez (github@swarminglogic.com)
Version: $version ($versionDate)
License: CC-zero (public domain)
"
exit $1
}
while test $# -gt 0; do
case "$1" in
-h|--help)
showHelp 0
;;
*)
hasId=yes
id=$1
shift
break
;;
esac
done
if [[ $hasId ]] ; then
tmpfile=/tmp/.ttic.${id}.time
else
tmpfile=/tmp/.ttic.time
fi
if [ ! -e $tmpfile ] ; then
echo "Did not find initalized time file. Run ttic with same id before ttoc!"
exit 1
fi
tic=`cat $tmpfile`
toc=$(($(date +%s%N)/1000000))
delta=$(($toc - $tic))
LC_NUMERIC=C LC_COLLATE=C
printf '%g\n' $(bc <<< "scale=3; ${delta}/1000")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment