0

The following will display the time it took for a given command to execute. How would I do the same but with better precision (i.e 5.23 seconds)?

[root@localhost ~]# start=`date +%s`; sleep 5 && echo execution time is $(expr `date +%s` - $start) seconds execution time is 5 seconds [root@localhost ~]# 

3 Answers 3

2

You could try using the time command.

time sleep 5 

In addition to elapsed wall clock time it will tell you how much CPU time the process consumed, and how much of the CPU time was spent in the application and how much in operating system calls.

Sign up to request clarification or add additional context in comments.

Comments

1

Use the time command:

time COMMAND 

Comments

0

You can also use the SECONDS variable:

In the Bash Variables section of the reference manual you'll read:

SECONDS This variable expands to the number of seconds since the shell was started. Assignment to this variable resets the count to the value assigned, and the expanded value becomes the value assigned plus the number of seconds since the assignment.

Hence, the following:

SECONDS=0; sleep 5; echo "I slept for $SECONDS seconds" 

should output 5. It's only good for measuring times with precision of 1 second, but it sure is much better than the way you showed using the date command.

If you need more precision, you can use the time command, but it's a bit tricky to get the output of this command in a variable. You'll find all the information in the BashFAQ/032: How can I redirect the output of time to a variable or file?.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.