104

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

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

9 Answers 9

133

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
Sign up to request clarification or add additional context in comments.

3 Comments

agreed with Maaza, this flexibility allowed me to measure the time of a whole set of commands.
Is it possible to apply linux aspects around whatever linux command to calculate execution
One disadvantage of this approach is that it measures in seconds, whereas time uses miliseconds. And if you want to time a handful of commands together without having to use variables and calculations, you can use parentheses: time ( command1 ; command2 ; command3 ) you may even time them individually together with a total time, like so: time ( time command1 ; time command2 ; time command3 ) Of course, if you want some twenty commands, might be better to use this answer's solution. But then, you should start thinking of putting your commands all in a script...
86

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

3 Comments

From man bash: "The TIMEFORMAT variable may be set to a format string that specifies how the timing information should be displayed"
Oops - I thought there was some way to format it, but I missed it in help time because I only glanced over it and I thought it was a command line argument. I'll update the answer.
Here we are 9 years later and the command only took 0m0.018s. I just thought I'd point that out.
37
root@hostname:~# time [command]

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

2 Comments

That is certainly the basic answer - but there are some complications if you want to see the error output from the command and yet send the timing information to standard output as the question does. The time command writes to stderr. What you suggest is probably accurate enough - hence my +1.
time writes to the tty, not stderr. Which I guess makes things worse.
16

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

Comments

9

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.

Comments

9

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`

Comments

8

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.

Comments

1

If you are using zshell, you can have zshell print the time @ the start and end of execution. You can accomplish this by adding the following in your ~/.zshrc:

# print time before & after every command
preexec() { eval THEDATE="`date +"[%D_%H:%M:%S] "`"; echo "<CMD STARTED> $THEDATE" } 
precmd()  { eval THEDATE="`date +"[%D_%H:%M:%S] "`"; echo "<CMD FINISHD> $THEDATE" }

and open a new terminal window to have the changes take effect in all future terminal sessions.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.