Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'd like to use the time command in a bash script to calculate the elapsed time of the script and write that to a log file. I only need the real time, not the user and sys. Also need it in a decent format. e.g 00:00:00:00 (not like the standard output). I appreciate any advice.

share|improve this question

5 Answers

up vote 40 down vote accepted

You could use the date command to get the current time before and after performing the work to be timed and calculate the difference like this:

#!/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))""

Notes: $((...)) can be used for basic arithmetic in bash – caution: do not put spaces before a minus - as this might be interpreted as a command-line option.

See also: http://tldp.org/LDP/abs/html/arithexp.html

EDIT:
Additionally, you may want to take a look at sed to search and extract substrings from the output generated by time.

EDIT:

Example for timing with milliseconds (actually nanoseconds but truncated to milliseconds here). Your version of date has to support the %N format and bash should support large numbers.

# 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}"
share|improve this answer
Seems incorrect: Time in seconds: 46 (OK), Pretty format: 00:00:00:46 (incorrect). Using time command: real 0m46.018s user 0m7.630s sys 0m1.190s – Robertico Sep 10 '10 at 11:19
Hm, I'm not quite sure what 00:00:00:00 is supposed to mean – I was guessing it meant [days]:[hours]:[minutes]:[seconds] ? That is what it shows, but you are free to adjust the code to calculate what you need. – Archimedix Sep 10 '10 at 12:02
excuses for the typo. Supposed to be 00:00:00.0000 (milliseconds) [hours]:[minutes]:[seconds].[milliseconds] – Robertico Sep 10 '10 at 12:23
@mmrobins, well, I don't know what was wrong with the line M="$((T%1000000000T/1000000))" that it was changed to M="$((T/1000000))" which in my opinion is wrong... without the modulo, you'd get the entire time in milliseconds, which is printed before the milliseconds. This does not make sense in my eyes, however the suggestion was apparently approved for some reasons unclear to me. – Archimedix Oct 14 '12 at 14:30
At least for me the problem was that it didn't work with the line using the modulo. I got the following error when run on multiple versions of bash on different flavors of Linux: bash: T%1000000000T: value too great for base (error token is "1000000000T"). – mmrobins Oct 16 '12 at 14:44
show 1 more comment

From the man page for time: 1)There may be a shell built-in called time, avoid this by specifying /usr/bin/time 2)You can provide a format string and one of the format options is elapsed time - %E

/usr/bin/time -f'%E' $CMD

Example:

/usr/bin/time -f'%E' ls /tmp/mako/
res.py  res.pyc
0:00.01
share|improve this answer
1  
That's why the command: time -f'%E' was not working, but /usr/bin/time -f'%E' works fine. Thx – Robertico Sep 10 '10 at 16:10
2  
Do you want to accept this as the answer? – Spaceghost Sep 11 '10 at 3:00

Use the bash built in variable SECONDS. Each time you reference the variable it will return the elapsed time since the script invocation.

Example:

echo "Start $SECONDS"

sleep 10

echo "Middle $SECONDS"

sleep 10

echo "End $SECONDS"

Output: Start 0 Middle 10 End 20

share|improve this answer

To use the Bash builtin time rather than /bin/time you can set this variable:

TIMEFORMAT='%3R'

which will output the real time that looks like this:

5.009

or

65.233

The number specifies the precision and can range from 0 to 3 (the default).

You can use:

TIMEFORMAT='%3lR'

to get output that looks like:

3m10.022s

The l (ell) gives a long format.

share|improve this answer

Not quite sure what you are asking, have you tried:

time yourscript | tail -n1 >log

Edit: ok, so you know how to get the times out and you just want to change the format. It would help if you described what format you want, but here are some things to try:

time -p script

This changes the output to one time per line in seconds with decimals. You only want the real time, not the other two so to get the number of seconds use:

time -p script | tail -n 3 | head -n 1
share|improve this answer
I want to include it in my script. – Robertico Sep 10 '10 at 11:30
Yes, if that was unambiguous then I wouldn't pointed out that it needs clarification. Why not use two scripts, one that calls the other? – Amoss Sep 10 '10 at 11:50
Because i've already 3 scripts. I saw an example like this: { time { # section code goes here } } 2> timing.log But i only need the real time, not the user and sys. Also need it in a decent format. e.g 00:00:00:00 (not like the standard output). – Robertico Sep 10 '10 at 11:58
ok, so you already know how to time a section, so that was just unnecessary confusion in your question. Is what you really want to know how to turn the time output into something easier to process, like just the number of seconds? – Amoss Sep 10 '10 at 16:42
yes i'd like to know how to turn the time output into something easier to process. – Robertico Sep 11 '10 at 5:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.