Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a three part answer</p> <h2>Part one</h2> <p>First, use the <code>TIMEFORMAT</code> variable to output only seconds elapsed. Then you can add this directly</p> <p>From <code>man bash</code></p> <blockquote> <p><strong>TIMEFORMAT</strong> The value of this parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word should be displayed. The % character introduces an escape sequence that is expanded to a time value or other information. The escape sequences and their meanings are as follows; the braces denote optional portions.</p> </blockquote> <p>Here is an example which outputs only seconds with a precision of 0, i.e. no decimal point. Read part three why that's important.</p> <pre><code>TIMEFORMAT='%0R'; time sleep 1 1 </code></pre> <h2>Part two</h2> <p>Second, how do we capture the output of <code>time</code>? It's actually a bit tricky, here's how you do capture the time from the command above</p> <pre><code>TIMEFORMAT='%0R'; time1=$( { time sleep 1; } 2&gt;&amp;1 ) </code></pre> <h2>Part three</h2> <p>How do we add the times together and get the average?</p> <p>In bash we use the <code>$(( ))</code> construct to do math. Note that <code>bash</code> does not natively support floating point so you will be doing integer division (hence the precision 0.) Here is a script that will capture the <code>time</code> from two commands and output each of the individual times and their average</p> <pre><code>#!/bin/bash TIMEFORMAT='%0R' time1=$( { time sleep 1; } 2&gt;&amp;1 ) time2=$( { time sleep 4; } 2&gt;&amp;1 ) ave=$(( (time1 + time2) / 2)) echo "time1 is $time1 | time2 is $time2 | average is $ave" </code></pre> <h3>Output</h3> <pre><code>time1 is 1 | time2 is 4 | average is 2 </code></pre> <p>If integer division is a non-starter for you and you want precision, as long as you don't mind calling the external binary <code>bc</code>, you can do this quite easily. </p> <pre><code>#!/bin/bash TIMEFORMAT='%3R' time1=$( { time sleep 1; } 2&gt;&amp;1 ) time2=$( { time sleep 4; } 2&gt;&amp;1 ) ave=$( bc &lt;&lt;&lt;"scale=3; ($time1 + $time2)/2" ) echo "time1 is $time1 | time2 is $time2 | average is $ave" </code></pre> <h3>Output</h3> <pre><code>time1 is 1.003 | time2 is 4.003 | average is 2.503 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload