Reputation: 26281
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 ~]#
Upvotes: 0
Views: 566
Reputation: 46813
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?.
Upvotes: 0
Reputation: 111219
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.
Upvotes: 2