zsh-users
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: dominik.vogt@gmx.de, Zsh Users <zsh-users@zsh.org>
Subject: Re: time command with shell builtins
Date: Tue, 24 Jan 2023 10:32:35 +0100	[thread overview]
Message-ID: <CAHYJk3Tu5POCU08-g1dr2p284p89Xak5XjLSV6J8gWdQnOXDeA@mail.gmail.com> (raw)
In-Reply-To: <Y87Ws8PrZY8W3Oao@localhost>

On 1/23/23, Dominik Vogt <dominik.vogt@gmx.de> wrote:
> On Mon, Jan 23, 2023 at 07:31:12PM +0100, Mikael Magnusson wrote:
>> On 1/23/23, Dominik Vogt <dominik.vogt@gmx.de> 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@gmx.de>
>> >> 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).

a)
  zle -N accept-line _accept_line
  function _accept_line () {
    zle .reset-prompt
  }

b)
  zmodload zsh/datetime
  function precmd_showtime() {
    if ! (( ${+_zsh_time} )); then return 0; fi
    if (( $EPOCHSECONDS - $_zsh_time > 10 )); then
      printf "%i seconds elapsed\n" $(( EPOCHSECONDS - _zsh_time ))
    fi
    unset _zsh_time
  }

  function preexec_recordtime() {
    typeset -g _zsh_time
    _zsh_time=$EPOCHSECONDS
  }

-- 
Mikael Magnusson


  reply	other threads:[~2023-01-24  9:33 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-23 13:40 Dominik Vogt
2023-01-23 13:42 ` Roman Perepelitsa
2023-01-23 14:17   ` zeurkous
2023-01-23 14:23     ` Roman Perepelitsa
2023-01-23 14:40       ` zeurkous
2023-01-23 14:28   ` Dominik Vogt
2023-01-23 14:46     ` zeurkous
2023-01-23 16:31     ` Bart Schaefer
2023-01-23 18:31     ` Mikael Magnusson
2023-01-23 18:49       ` Dominik Vogt
2023-01-24  9:32         ` Mikael Magnusson [this message]
2023-01-24 10:48           ` Dominik Vogt
2023-01-24 23:12             ` Dominik Vogt
2023-01-24 23:36               ` Bart Schaefer
     [not found]                 ` <Y9B7A8dWLiZNXKfW@localhost>
2023-01-26 16:23                   ` Dominik Vogt
2023-01-26 16:56                     ` Bart Schaefer
2023-01-26 17:26                       ` Dominik Vogt
2023-01-26 17:40                         ` Bart Schaefer
2023-02-02 18:10                     ` Dominik Vogt
2023-02-02 18:28                       ` Bart Schaefer
2023-02-02 19:15                         ` Dominik Vogt
2023-02-02 19:15                           ` Dominik Vogt
2023-02-02 19:31                           ` Bart Schaefer
2023-01-24 23:32             ` Bart Schaefer
2023-01-25  7:43               ` Mikael Magnusson
2023-01-25 12:58                 ` Dominik Vogt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHYJk3Tu5POCU08-g1dr2p284p89Xak5XjLSV6J8gWdQnOXDeA@mail.gmail.com \
    --to=mikachu@gmail.com \
    --cc=dominik.vogt@gmx.de \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).