Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use the <code>date</code> command to get the current time before and after performing the work to be timed and calculate the difference like this:</p> <pre><code>#!/bin/bash # Get time as a UNIX timestamp (seconds elapsed since Jan 1, 1970 0:00 UTC) T="$(date +%s)" # Do some work here sleep 2 T="$(($(date +%s)-T))" echo "Time in seconds: ${T}" printf "Pretty format: %02d:%02d:%02d:%02d\n" "$((T/86400))" "$((T/3600%24))" "$((T/60%60))" "$((T%60))"" </code></pre> <p>Notes: <strong>$((...))</strong> can be used for basic arithmetic in <em>bash</em> &ndash; caution: do not put spaces before a minus <strong>-</strong> as this might be interpreted as a command-line option.</p> <p>See also: <a href="http://tldp.org/LDP/abs/html/arithexp.html" rel="noreferrer">http://tldp.org/LDP/abs/html/arithexp.html</a></p> <p><em>EDIT:</em><br> Additionally, you may want to take a look at <a href="http://www.google.com/search?hl=en&amp;q=sed" rel="noreferrer"><strong>sed</strong></a> to search and extract substrings from the output generated by <strong>time</strong>.</p> <p><em>EDIT:</em> </p> <p>Example for timing with milliseconds (actually nanoseconds but truncated to milliseconds here). Your version of <code>date</code> has to support the <code>%N</code> format and <code>bash</code> should support large numbers.</p> <pre><code># UNIX timestamp concatenated with nanoseconds T="$(date +%s%N)" # Do some work here sleep 2 # Time interval in nanoseconds T="$(($(date +%s%N)-T))" # Seconds S="$((T/1000000000))" # Milliseconds M="$((T/1000000))" echo "Time in nanoseconds: ${T}" printf "Pretty format: %02d:%02d:%02d:%02d.%03d\n" "$((S/86400))" "$((S/3600%24))" "$((S/60%60))" "$((S%60))" "${M}" </code></pre> <p><strong>DISCLAIMER:</strong><br> My original version said</p> <pre><code>M="$((T%1000000000/1000000))" </code></pre> <p>but this was edited out because it apparently did not work for some people whereas the new version reportedly did. I did not approve of this because I think that you have to use the remainder only but was outvoted.<br> Choose whatever fits you.</p>
    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