Advertisement
pintcat

stopwatch - measures time between a certain start & end point

Mar 17th, 2024 (edited)
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.61 KB | Software | 0 0
  1. ############################################################################################################################
  2. # stopwatch v1.5 - simple tool to measure time between a certain start & end point                                         #
  3. # Handshake:                                                                                                               #
  4. # $SW_PREC sets the precision level. Higher values result in larger, more precise measurements (default: 3)                #
  5. # Usage:                                                                                                                   #
  6. # Simply call SW_TRIGGER() to start measuring. If called the first time, it sets the current time as start time. When      #
  7. # called every other time, it calculates and shows the difference between start and current time and also sets the current #
  8. # time as new start time. Note that the function itself takes about 10-30ms of processing time. Prints results to stdout.  #
  9. ############################################################################################################################
  10.  
  11. SW_START=
  12.  
  13. SW_TRIGGER(){
  14.     [ -z "${SW_PREC##*[!0-9]*}" ] || [ $SW_PREC -lt 3 ] && SW_PREC=3
  15.     SW_MULT=1$(printf %"$SW_PREC"s | tr " " "0")
  16.     SW_TIME=$(date +%s:%"$SW_PREC"N | awk -F ':' '{print ($1*'$SW_MULT')+$2}')
  17.     if [ -n "$SW_START" ]; then
  18.         SW_DIFF=$(($(date +%s:%"$SW_PREC"N | awk -F ':' '{print ($1*'$SW_MULT')+$2}')-$SW_START))
  19.         [ $SW_PREC -gt 3 ] && SW_DIFF=$(printf $SW_DIFF | awk '{printf("%."SW_DP"f\n",$1/10^SW_DP)}' SW_DP=$(($SW_PREC-3)))
  20.         echo $SW_DIFF"ms"
  21.     fi
  22.     SW_START=$SW_TIME
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement