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.
Just thought I'd post my 2 cents. I think you want
date +%s%0N
overdate+%s%N
, unless I'm missing something; otherwise nanoseconds are printed by %N without leading zeroes, independently of the %s. If you're using nanos as fractional seconds (aka fixed point arithmetic with ns as the atomic unit) the leading zeroes become significant. So for example, 1s+108ns = "1100000000", 1s+107ns = "110000000", ..., 1s+1ns = "11". (You want 1s+108ns = "1100000000", 1s+107ns = "1010000000", ..., 1s+1ns = "1000000001".) You can expect 10% of measurements to have at least 1 leading zero in the nanos, and each omitted zero to cause an order of magnitude of error in the total time. Hope this helps!