Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: profile prompt rendering time



On Jul 13,  4:31pm, Filipe Silva wrote:
}
} Hi, Bart. thanks for the help. I can work with the execution time of ls
} included, no problem.

If you use precmd + zle-line-init then you don't need to.  Just saying.

} But I did not really understand where I have to declare SECONDS.

Anywhere in your .zshrc or similar init file, or at the command line,
as long as it happens before the first event for which you want to
get the timings.

} See if I understand correctly. I'll open my zshrc and type this:
} 
} ```
} preexec() {
}   float SECONDS
} }
} precmd() {
}   print $SECONDS
} }
} ```
} 
} is that right?

Not quite.  To spell out the example:

    float SECONDS
    preexec() { print BEFORE COMMAND: $SECONDS }
    precmd() { print BEFORE PROMPT: $SECONDS }
    zle-line-init() { zle -M "AFTER PROMPT: $SECONDS" }
    zle -N zle-line-init

This will give e.g.:

torch% echo hello
BEFORE COMMAND: 7.365769200e+01
hello
BEFORE PROMPT: 7.365787900e+01                                                  
torch% 
AFTER PROMPT: 7.365904200e+01

You don't really need both preexec and precmd, I included them to
show the difference.  You DO need zle-line-init, there's no other
place to read the clock immediately after the prompt is displayed.

} Also, is 4.800e+2 = 480 milliseconds?

$SECONDS is the elapsed time in seconds since the shell started.  So
you have to subtract to get the delta.  Maybe this is better:

    float START SECONDS
    precmd() { START=$SECONDS }
    zle-line-init() { zle -M "ELAPSED: $((SECONDS - START)) seconds" }

Silly example to demonstrate:

torch% setopt promptsubst; PS1='$(sleep 3)'"$PS1"
torch% 
ELAPSED: 3.015070000000037 seconds



Messages sorted by: Reverse Date, Date, Thread, Author