Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: time command with shell builtins
- X-seq: zsh-users 28762
- From: Dominik Vogt <dominik.vogt@xxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: time command with shell builtins
- Date: Tue, 24 Jan 2023 11:48:39 +0100
- Archived-at: <https://zsh.org/users/28762>
- In-reply-to: <CAHYJk3Tu5POCU08-g1dr2p284p89Xak5XjLSV6J8gWdQnOXDeA@mail.gmail.com>
- 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>
- Reply-to: dominik.vogt@xxxxxx
- Ui-outboundreport: notjunk:1;M01:P0:mn57CxFjQBo=;raZa0qTv4aGgazPRvvZRBPIzCKP CORGLWaU+QLwpZbcOyucgzdsw9VoUGSazMh5tt3B+mxo7RUrPVl3hbmnuJqfSOrclD6UEHTVu lGwryP1xUtPs4l7HdbAhmqlljcD9CcDMCTiubJYK99cfEtdevW/vuA9oABNaRskt2nM7jCZzm Te1EURtX1jWwjOEQPnB8kNuV3DqAWir+1G09Y50Xz+vBjqBAX9GiSSD58xFB7j0Zo0Nd4tozO CW/gaS9w5jd89/LDK8TuCGXU5BVHR4cmBraWi/jAfg53Jna5sAq/bqVpMztmSc8UJC8PZYr9B bRxgjBvUbRzDOWhDRFYiXLwYhlzaHSOQ+K8+mLBjbGuMg50WeeuuyG0Uw8mIAjPAOuq1eFCV1 s5vAd3Qjpz3sMu6ZvZBTTfVn75vugsmsS1o++4TppQNnzYP2jkc8wLa+0aXHZjCRvA1h0KaP7 KmrqsP1H9eCwOP39vCbdfEZ1qK6Nodyt2qVlp2sArgX4oVT4PG4qW0iq/T1dIb5RlGQdb3XK9 fLItkYiI27ycAyhg4YhHdn03eydh6D7BzLviHJwEa7Cfq4wLAHpJT8PnYYTe6/2fGk3eNxtSr lRwkdTi1CHo7696a4SV1iVbXKqQpJi/yNIY2uV2Wqb7VHpCCHX7KM+EQg9fZP1338gn74ZQ/f Bgjw+4cZzSQqHBNCu/doIzsjOMTl1cFDPpABgF1VT6qaLVEDn01bYfAIUuMvQjC7Qzl60asDJ V+OJE0Z5X8LXThEuQcqpVh94OY84m39BgFz7twPAHqzJrQ6+7xG2eEfuIclgMmFNRzDF7psnF coPmiz8VS62yozOJTQcYsKWro6CEgK15vr1UzooVBifmazLnne9TEs2EtIebfsnIXSwhxa9iU xFFJRzfiNuaJVFRN/q2oGQy1Xq5b8piOHBFVEE58v6NLHU0qpKIZdKmpkl3F8ie8r/+qkp8bv Jg9CFQ==
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 --
The measurement is good enough at the moment. Putting the result
in the prompt has the advantage of not polluting the terminal.
Ciao
Dominik ^_^ ^_^
--
Dominik Vogt
Messages sorted by:
Reverse Date,
Date,
Thread,
Author