Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: time command with shell builtins
- X-seq: zsh-users 28772
- From: Dominik Vogt <dominik.vogt@xxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: time command with shell builtins
- Date: Wed, 25 Jan 2023 00:12:12 +0100
- Archived-at: <https://zsh.org/users/28772>
- In-reply-to: <Y8+3h5OXqtpkyewM@localhost>
- List-id: <zsh-users.zsh.org>
- Mail-followup-to: zsh-users@xxxxxxx
- References: <Y86OR0DCpwfSYCXU@localhost> <CAN=4vMqSxEr-K1xg+fGc9kh2qfiZ31fy7hoDBRNHXXF6pH8eEg@mail.gmail.com> <Y86ZkYooJ9Kf4xCE@localhost> <CAHYJk3QD8NbCqt0LdXshiVf6W3GQLnDzo6-1mj0qCJOTkHFGRA@mail.gmail.com> <Y87Ws8PrZY8W3Oao@localhost> <CAHYJk3Tu5POCU08-g1dr2p284p89Xak5XjLSV6J8gWdQnOXDeA@mail.gmail.com> <Y8+3h5OXqtpkyewM@localhost>
- Reply-to: dominik.vogt@xxxxxx
- Ui-outboundreport: notjunk:1;M01:P0:ndNag2eW4gI=;A9rdUas6wyyHog2xc0v1juD0LUx YoG7wJ+MvkbIfVuybymZGy0safMsuwCBH+hOxyopPqMqzJQ+kSXIFfKbzweUBt3ZU6TsnK/Nx ds/HBMQC+NX29jtkg4CUemnN1W/zfSE0cLn2d/iVRb4YVWyQUK7/4jNdgO+SblRex2m3IiEdw gA8GpxdQv/D/BYRfZfLZlL7bcIGz3mjkFG/8cLumOIo7MtpkG159Loai5Y67DsfDOzwVRbx2T HgTsygpKPEf+EK07RGOWELH6AC00F7TYi6Mmvii3loIqu2SuWQ5spTbwifVQVYVbu1hwrFm54 7S4IBEVp0qP+03Dnogo/l/vrN9q8VtxuLYlAmVryBi5uHzwvB+PfeEt3RmXHuAUyZZjW3QWre G+oQU4SEJzF7Qs2czkz6i9Pu85R3NKL5XvROXE1BX/QiULoi8S80SsTVuEd0+UZLhfncn5zQE rpJK/vCszOZXFNDoyGA6OxytFRdyCw2bwflmdnNcAoZ7lTWJa0Fu6poNrcGGz73gRlk910vhl 5gm1rLP8A/aR8P8uXnMnf/g3rg3muSvWdMHtcF3Oa3VR9Vx+pWH27Jtp8DDfRZ23fKnFDTeJC g9GwyzhVyCUoRJQy1dP3lqf0w46RhWmR9LSv6/aeExZfJ/+CV3B+bEYubq1y+mUcfo4/jLCjk iA3vWUfeCVgVXAJCGCY9vhGWGzlBI8pk/leLmMrX5+IEpYtrcaCka3FL3FAbWyuswUQP/cop6 DeM+6OHZB5ka20JkWALkteVPJkaiDdWqudvHZUFYsoWDPPIpPiBa1ttVqt1t1w6TyQ1p6vjlZ dQUSMglZPkrY7zUJqz1g8Xw14G4LXGBmZb7Bpp/amFxZiJfGo11pS6Fi5z7/GZkq3qVTYZezm u8lZo4/rYxnCYSNlXxZB8iZh4S5SKo6zufqBie7ejadHxYRH6TK7tRei+YkW3iNDRpGoEDPQX 8J1ksIIWzvXxfPHRv75xiLJsSrI=
On Tue, Jan 24, 2023 at 11:48:39AM +0100, Dominik Vogt wrote:
> On Tue, Jan 24, 2023 at 10:32:35AM +0100, Mikael Magnusson wrote:
> > On 1/23/23, Dominik Vogt <dominik.vogt@xxxxxx> wrote:
> > > On Mon, Jan 23, 2023 at 07:31:12PM +0100, Mikael Magnusson wrote:
> > >> On 1/23/23, Dominik Vogt <dominik.vogt@xxxxxx> wrote:
> > >> > On Mon, Jan 23, 2023 at 02:42:05PM +0100, Roman Perepelitsa wrote:
> > >> >> On Mon, Jan 23, 2023 at 2:40 PM Dominik Vogt <dominik.vogt@xxxxxx>
> > >> >> wrote:
> > >> >> >
> > >> >> > Is it possible to get timing statistics of shell builtins too?
> > >> >> > Timing "echo" isn't very interesting, but timing loop constructs
> > >> >> > would be:
> > >> >> >
> > >> >> > $ time while foo; do bar done
> > >> >>
> > >> >> This:
> > >> >>
> > >> >> % time ( while foo; do bar; done )
> > >> >
> > >> > That wasn't really the question. Of course I can time a loop by
> > >> > writing a different command, or by putting it in a pipe or file.
> > >> >
> > >> > $ time echo foo | true
> > >> >
> > >> > I just want to get timing statistics of loops either explicitly by
> > >> > prepending "time" or implicitly with REPORTTIME.
> > >>
> > >> As Bart already mentioned, the answer to your question is "no", but
> > >> you can avoid some downsides of the subshell (eg, if your loop has
> > >> side effects that are relevant to the rest of the script etc), by
> > >> using SECONDS:
> > >> % () { typeset -F4 SECONDS=0; sleep 1; () { typeset -F3 SECONDS=0;
> > >> sleep 0.43; echo $SECONDS }; sleep 1; echo $SECONDS }
> > >> 0.431
> > >> 2.4329
> > >> (the downside here is obviously that it doesn't split out cpu/system
> > >> time for you, only elapsed time).
> > >
> > > Well, the worst downside for me is that REPORTTIME does not work.
> > > The use case is "oh, that command ran a long time, I'd really
> > > like to know how long it took". I see no solution for that if
> > > re-running the command is no optiuon because it takes too long.
> > >
> > > At the moment I'm writing some automation scripts that run for
> > > hours and print their progress. I might want to kill them after a
> > > few hours and see how many seconds they ran and compare it to the
> > > progress output.
> >
> > What you can do at the moment is a) put the time in your prompt (and
> > reset the prompt on accept-line), b) save the current time in preexec
> > and compare it against the current time in precmd and print it out if
> > it exceeds some threshold(, c) or both).
>
> Good idea. This is what I use now:
>
> -- snip --
> autoload -Uz add-zsh-hook
> zmodload zsh/datetime
>
> function preexec_recordtime() {
> typeset -g _zsh_time
> _zsh_time="$EPOCHSECONDS"
> }
> add-zsh-hook preexec preexec_recordtime
>
> function __prompt_get_displaytime () {
> if ! (( ${+_zsh_time} )); then return 0; fi
> if ! (( ${+REPORTTIME} )); then return 0; fi
> if (( $EPOCHSECONDS - $_zsh_time > $REPORTTIME )); then
> printf " $[EPOCHSECONDS - _zsh_time]s"
> fi
> unset _zsh_time
> }
>
> PS1="<...>$(__prompt_get_displaytime)<...>"
> -- snip --
Hm, any idea how to suppress reporting the run time of interactive
commands like vi, emacs, mutt? Not very important, but it's a bit
confusing. As far as I understand there's no reliable access to
which command is being executed in preexec. I could replace such
commands by functions that erase _zsh_time when they start...
Ciao
Dominik ^_^ ^_^
--
Dominik Vogt
Messages sorted by:
Reverse Date,
Date,
Thread,
Author