bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: feature: time builtin and file descriptor


From: Geir Hauge
Subject: Re: feature: time builtin and file descriptor
Date: Thu, 30 Oct 2014 13:53:20 +0100

2014-10-29 15:09 GMT+01:00 Sami Kerola <kerolasa@iki.fi>:
>
> This idea came to my mind while writing a script that runs multiple
> commands, and I simply wanted to know how long they are busy.  I am
> aware alternatives exist, but they can get a bit tricky if one wants to
> have multiple measurements going on simultaneously.  For example:
>
> exec 42>&0
> time --file-descriptor 42
> for i in items; in
>         exec 52>&0
>         time --file-descriptor 52
>         echo "processing $i"
>         [...]
>         echo "item $i took"
>         exec 52>&-
> done
> echo 'all together took:'
> exec 42>&-
>

You could achieve this by using command grouping and TIMEFORMAT:

items=( a b c )
time {
    for i in "${items[@]}"; do
        time {
            echo "processing $i"
            sleep "$((RANDOM%5 + 1))"
            TIMEFORMAT="item $i took %R seconds"
        }
    done
    TIMEFORMAT='all together took: %R seconds'
}

example output:

processing a
item a took 5.001 seconds
processing b
item b took 1.001 seconds
processing c
item c took 3.001 seconds
all together took: 9.004 seconds


It would be prettier if TIMEFORMAT could be set on invocation of time,
e.g.  TIMEFORMAT='foo took %R seconds' time { ...; } , but time being a
keyword probably makes that hard.

One also has to be careful to sanitize any variables one embeds in
TIMEFORMAT since % characters are special.

-- 
Geir Hauge


reply via email to

[Prev in Thread] Current Thread [Next in Thread]