• Re: Increasing "time" command precision

    From Thomas Schmitt@21:1/5 to Franco Martelli on Wed Aug 7 22:00:01 2024
    Hi,

    Franco Martelli wrote:
    ~# export TIMEFORMAT='real %3R
    user %3U
    sys %3S'

    The manual and the source code say that the format variable is "TIME"
    not "TIMEFORMAT".
    https://sources.debian.org/src/time/1.9-0.2/src/time.c/#L656

    It seems to understand the two characters "\n" as line break.
    Further: "real" would better match "%e" or "%E",

    $ export TIME="real %e sec\nuser %U sec\nsys %S sec"
    $ /usr/bin/time echo

    real 0.00 sec
    user 0.00 sec
    sys 0.00 sec
    $

    The number formats are hardcoded:

    https://sources.debian.org/src/time/1.9-0.2/src/time.c/#L526
    case 'S': /* System time. */
    fprintf (fp, "%ld.%02ld",

    https://sources.debian.org/src/time/1.9-0.2/src/time.c/#L531
    case 'U': /* User time. */
    fprintf (fp, "%ld.%02ld",

    https://sources.debian.org/src/time/1.9-0.2/src/time.c/#L552
    case 'e': /* Elapsed real time in seconds. */
    fprintf (fp, "%ld.%02ld",

    There is no interpreter to see for adjustable number precision.


    Have a nice day :)

    Thomas

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Schmitt@21:1/5 to All on Wed Aug 7 22:20:02 2024
    Hi,

    it turns out that TIMEFORMAT belongs to the bash builtin "time"
    and that digit precision numbers only up to 3 are obeyed.

    $ export TIMEFORMAT="r=%7R
    u=%4U
    s=%1S"
    $ time echo

    r=0.000
    u=0.000
    s=0.0
    $

    man bash:

    %[p][l]R The elapsed time in seconds.
    [...]
    The optional p is a digit specifying the precision, the number
    of fractional digits after a decimal point. A value of 0 causes
    no decimal point or fraction to be output. At most three places
    after the decimal point may be specified; values of p greater
    than 3 are changed to 3. If p is not specified, the value 3 is
    used.


    Have a nice day :)

    Thomas

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to Thomas Schmitt on Wed Aug 7 22:30:01 2024
    On Wed, Aug 07, 2024 at 22:15:00 +0200, Thomas Schmitt wrote:
    it turns out that TIMEFORMAT belongs to the bash builtin "time"
    and that digit precision numbers only up to 3 are obeyed.

    $ export TIMEFORMAT="r=%7R
    u=%4U
    s=%1S"
    $ time echo

    r=0.000
    u=0.000
    s=0.0

    Just for the record:

    * You can use the $'...' quoting form in bash, to put newlines in
    a string value using \n.

    * TIMEFORMAT=... time foo will invoke /usr/bin/time, but
    TIMEFORMAT=... eval time foo will use the builtin.

    hobbit:~$ TIMEFORMAT=$'real %R\nuser %U\nsys %S' eval time sleep 0.2
    real 0.202
    user 0.002
    sys 0.001

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Schmitt@21:1/5 to Greg Wooledge on Thu Aug 8 08:40:01 2024
    Hi,

    Greg Wooledge wrote:
    * TIMEFORMAT=... time foo will invoke /usr/bin/time, but
    TIMEFORMAT=... eval time foo will use the builtin.

    I wonder about the formal reason for this.

    Is it because "time" is not listed in the man page under
    SHELL BUILTIN COMMANDS but mentioned as "reserved word" under Pipelines ?
    Does the variable assignment before "time" cause the rest of the line
    to be regarded as Simple Command rather than as Pipeline ?


    Have a nice day :)

    Thomas

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to Thomas Schmitt on Thu Aug 8 13:30:01 2024
    On Thu, Aug 08, 2024 at 08:31:10 +0200, Thomas Schmitt wrote:
    Greg Wooledge wrote:
    * TIMEFORMAT=... time foo will invoke /usr/bin/time, but
    TIMEFORMAT=... eval time foo will use the builtin.

    (Yeah, oops, time is a "shell keyword", not a "builtin".)

    I wonder about the formal reason for this.

    Is it because "time" is not listed in the man page under
    SHELL BUILTIN COMMANDS but mentioned as "reserved word" under Pipelines ? Does the variable assignment before "time" cause the rest of the line
    to be regarded as Simple Command rather than as Pipeline ?

    There are some mysteries in bash that I'm content simply to write off as
    "here be magic". I note that such mysteries exist, I try to find the
    best workarounds that I can, and I document them if I feel other users
    are likely to find my workarounds helpful.

    Honestly, I don't fully know. You could ask on the help-bash mailing
    list, if you really want to find out. Or go source-diving in bash
    yourself. But I suspect it's simply that the grammar for "time" was
    never extended to allow temporary variable assignments.

    Shell keywords (such as "time", and also "if", "while", "case", "[["
    and several others) have special grammar/parsing rules. If one of these keywords is involved, you're no longer looking at a Simple Command.
    You've got something different.

    In the specific case of "time", the syntax is

    time [-p] pipeline

    If your code says

    time echo hi | sleep 2

    then you aren't simply timing the echo command. You're timing the whole *pipeline*, including the sleep command. This wouldn't be possible
    without "time" flexing its keyword powers and bending the parsing rules.
    You can't do that with /usr/bin/time, unless you invoke a whole new shell:

    /usr/bin/time sh -c 'echo hi | sleep 2'

    which of course distorts your timing. (Also note that "time echo" runs
    the shell's builtin echo, while "/usr/bin/time echo" would be forced
    to run /usr/bin/echo, since /usr/bin/time doesn't have access to the
    shell's builtins.)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Schmitt@21:1/5 to Greg Wooledge on Thu Aug 8 14:20:01 2024
    Hi,

    Greg Wooledge wrote:
    * TIMEFORMAT=... time foo will invoke /usr/bin/time, but
    TIMEFORMAT=... eval time foo will use the builtin.

    I wrote:
    I wonder about the formal reason for this.

    There are some mysteries in bash that I'm content simply to write off as "here be magic".

    At least i know by now why i got to see two output formats of "time"
    during the last 30 years of GNU/Linux programming

    If not for the tangible help, i cherrish this list for the background information which floats along here.


    Have a nice day :)

    Thomas

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Wooledge@21:1/5 to Franco Martelli on Thu Aug 8 15:20:01 2024
    On Thu, Aug 08, 2024 at 15:08:33 +0200, Franco Martelli wrote:
    The Bash's shell keyword "time" it could be fine, but I don't know how to redirect its output to a file (-o switch of /usr/bin/time).

    https://mywiki.wooledge.org/BashFAQ/032

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)