Elapsed and execution time display for commands in ZSH
Append this to your ~/.zshrc file.
function preexec() {
timer=$(($(date +%s%0N)/1000000))
}
function precmd() {
if [ $timer ]; then
now=$(($(date +%s%0N)/1000000))
elapsed=$(($now-$timer))
export RPROMPT="%F{cyan}${elapsed}ms %{$reset_color%}"
unset timer
fi
}
Remixed from @adri's snippet.
Here's my take on the matter,
uses
date +%s%3N
to get milliseconds as base unit,so no need to resort to division and consequent slimmer code:
--
Longer version for the timer to adapt to the command execution length (h/m/s/ms) and always have 3 digits accuracy.
Adapted from https://stackoverflow.com/questions/1862510/how-can-the-last-commands-wall-time-be-put-in-the-bash-prompt