Print execution time of a shell command

ShellCommand Line

Shell Problem Overview


Is is possible to print the execution time of a shell command with following combination?

root@hostname:~# "command to execute" && echo "execution time"

Shell Solutions


Solution 1 - Shell

time is a built-in command in most shells that writes execution time information to the tty.

You could also try something like

start_time=`date +%s`
<command-to-execute>
end_time=`date +%s`
echo execution time was `expr $end_time - $start_time` s.

Or in bash:

start_time=`date +%s`
<command-to-execute> && echo run time is $(expr `date +%s` - $start_time) s

Solution 2 - Shell

Don't forget that there is a difference between bash's builtin time (which should be called by default when you do time command) and /usr/bin/time (which should require you to call it by its full path).

The builtin time always prints to stderr, but /usr/bin/time will allow you to send time's output to a specific file, so you do not interfere with the executed command's stderr stream. Also, /usr/bin/time's format is configurable on the command line or by the environment variable TIME, whereas bash's builtin time format is only configured by the TIMEFORMAT environment variable.

$ time factor 1234567889234567891 # builtin
1234567889234567891: 142662263 8653780357

real	0m3.194s
user	0m1.596s
sys	0m0.004s
$ /usr/bin/time factor 1234567889234567891
1234567889234567891: 142662263 8653780357
1.54user 0.00system 0:02.69elapsed 57%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+215minor)pagefaults 0swaps
$ /usr/bin/time -o timed factor 1234567889234567891 # log to file `timed`
1234567889234567891: 142662263 8653780357
$ cat timed
1.56user 0.02system 0:02.49elapsed 63%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+217minor)pagefaults 0swaps

Solution 3 - Shell

root@hostname:~# time [command]

It also distinguishes between real time used and system time used.

Solution 4 - Shell

For a line-by-line delta measurement, try gnonom.

> It is a command line utility, a bit like moreutils's ts, to prepend timestamp information to the standard output of another command. Useful for long-running processes where you'd like a historical record of what's taking so long. > > Piping anything to gnomon will prepend a timestamp to each line, indicating how long that line was the last line in the buffer--that is, how long it took the next line to appear. By default, gnomon will display the seconds elapsed between each line, but that is configurable.

gnomon demo

Solution 5 - Shell

Adding to @mob's answer:

Appending %N to date +%s gives us nanosecond accuracy:

start=`date +%s%N`;<command>;end=`date +%s%N`;echo `expr $end - $start`

Solution 6 - Shell

In zsh you can use

=time ...

In bash or zsh you can use

command time ...

These (by different mechanisms) force an external command to be used.

Solution 7 - Shell

If I'm starting a long-running process like a copy or hash and I want to know later how long it took, I just do this:

$ date; sha1sum reallybigfile.txt; date

Which will result in the following output:

Tue Jun  2 21:16:03 PDT 2015
5089a8e475cc41b2672982f690e5221469390bc0  reallybigfile.txt
Tue Jun  2 21:33:54 PDT 2015

Granted, as implemented here it isn't very precise and doesn't calculate the elapsed time. But it's dirt simple and sometimes all you need.

Solution 8 - Shell

Just ps -o etime= -p "<your_process_pid>"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjackView Question on Stackoverflow
Solution 1 - ShellmobView Answer on Stackoverflow
Solution 2 - ShellMark RushakoffView Answer on Stackoverflow
Solution 3 - ShellJasonView Answer on Stackoverflow
Solution 4 - ShellJanus TroelsenView Answer on Stackoverflow
Solution 5 - ShellnavView Answer on Stackoverflow
Solution 6 - ShellüberRegenbogenView Answer on Stackoverflow
Solution 7 - ShellEthanView Answer on Stackoverflow
Solution 8 - ShellAlejandro GaleraView Answer on Stackoverflow