Skip to content

Instantly share code, notes, and snippets.

@pepoluan
Created December 6, 2025 06:26
Show Gist options
  • Select an option

  • Save pepoluan/684c72bd4597f47baa6c894501544fef to your computer and use it in GitHub Desktop.

Select an option

Save pepoluan/684c72bd4597f47baa6c894501544fef to your computer and use it in GitHub Desktop.
Track elapsed time in milliseconds on ZSH
# This code snippet is dedicated to the Public Domain, or licensed under CC0 if you prefer.
printf -v _start '%.0f' $(( EPOCHREALTIME * 1000 ))
::
::: COMMANDS TO MEASURE HERE :::
::
printf -v _end '%.0f' $(( EPOCHREALTIME * 1000 ))
_elapsed=$(( _end - _start ))
# Bonus: Print elapsed time in unit of seconds:
# The space " " after the colon ":" in the second substitution is REQUIRED!
# Without the space, it will be interpreted as ":-" (substitute if none) rather than ":" (substring)
echo "Those commands took ${_elapsed::-3}.${_elapsed: -3} seconds to complete."
# If you dislike trailing zeroes after the decimal separator, process _elapsed first.
# I shall leave that to you because I personally don't see the need.
@pepoluan

pepoluan commented Dec 6, 2025

Copy link
Copy Markdown
Author

Please note that THIS DOES NOT WORK IN bash !!

The simple reason being that bash does NOT support floating-point arithmetics within (( )), so Lines 3 & 7 there will result in a Syntax Error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment