Created
December 6, 2025 06:26
-
-
Save pepoluan/684c72bd4597f47baa6c894501544fef to your computer and use it in GitHub Desktop.
Track elapsed time in milliseconds on ZSH
This file contains hidden or 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
| # 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. |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please note that THIS DOES NOT WORK IN
bash!!The simple reason being that
bashdoes NOT support floating-point arithmetics within(( )), so Lines 3 & 7 there will result in a Syntax Error.