256

I'm trying to measure the execution time of a process that I call via the command line (i.e., I want to find out how long it takes to for the process to finish). Is there any command that I can add to the command calling the process that will achieve this?

htorque
  • 64,798

9 Answers9

303

Add time before the command you want to measure. For example: time ls.

The output will look like:

real    0m0.606s
user    0m0.000s
sys     0m0.002s

Explanation on real, user and sys (from man time):

  • real: Elapsed real (wall clock) time used by the process, in seconds.
  • user: Total number of CPU-seconds that the process used directly (in user mode), in seconds.
  • sys: Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
Daniel
  • 758
  • 6
  • 16
ninjalj
  • 3,131
  • 1
    @ninjalj, can you provide more information on what the real, user, and sys times are that this command returns? – Knowledge Cube Jul 17 '11 at 20:22
  • 2
    @JacobVlijm This answer isn't that elaborate. :) You could edit in your comment and make it so. – muru Feb 13 '15 at 16:43
  • 1
    Note that you may need sudo apt-get install time if you are using a shell where time is not a builtin. – poolie Oct 26 '16 at 16:45
  • 1
    Note that this is the output from Bash's time builtin, but man time would be about an executable (like /usr/bin/time, from the time package), and its output would look different. Also in Bash, you can run help time for help with the builtin. – wjandrea Mar 25 '19 at 21:08
  • Note that the process end does not mean all the work is finished. A copy may take additional minutes after the "time" returns for flushing system bufffers (therefor with unallocated sys time also). – ubfan1 Jun 23 '19 at 16:25
  • OMG! It is integrated in MacOS zhs already – canbax Feb 22 '23 at 12:04
  • For some reason, time echo foo don't display the time. Is there a way to display the time independent of the command when using time? – Vencovsky Apr 12 '23 at 07:39
  • fish shell users: time is a fish builtin. If you want the standard version on your system, use command time ... – BallpointBen Dec 08 '23 at 20:48
64

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

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

32

You can use time:

time ls -R
11
date +"%T" && cp -r ./file  /destination/folder/here && date +"%T"

Running this command in the terminal will give you the total time for coping a file

Parto
  • 15,325
  • 24
  • 86
  • 117
  • 2
    This will give you the start time and end time, not the duration. – wjandrea Mar 25 '19 at 19:31
  • This is a good answer, under some circumstances. For example, the following find command -- without the 2>/dev/null redirect -- gives copious Permission denied messages. However, adding 2>/dev/null to that command breaks the time portion of that command. The following provides a good compromise: START="$(date +"%s")" && find 2>/dev/null / -path /mnt -prune -o -name "*libname-server-2.a*" -print; END="$(date +"%s")"; TIME="$((END - START))"; printf 'find command took %s sec\n' "$TIME", giving (e.g.) /usr/lib/libname-server-2.a find command took 3 sec as the sole output. – Victoria Stuart May 22 '19 at 03:45
  • An addendum to my comment: of course, you can simply run time sudo find / -path /mnt -prune -o -name "*libname-server-2.a*" -print (i.e., as sudo) -- avoiding those numerous Permission denied warnings. – Victoria Stuart May 22 '19 at 16:04
6

Occasionally I find myself needing a stopwatch to count how long it takes for an action like my app booting, in which case many of the solutions here are not useful.

For this I like to use sw.

sw

Install

wget -q -O - http://git.io/sinister | sh -s -- -u https://raw.githubusercontent.com/coryfklein/sw/master/sw

Usage

sw
 - start a stopwatch from 0, save start time in ~/.sw
sw [-r|--resume]
 - start a stopwatch from the last saved start time (or current time if no last saved start time exists)
 - "-r" stands for --resume
3
time -v command

-v gives more information

3

In zsh, the time command's output is slightly different.

To interpret the output:

time sleep 10
sleep 10  0.00s user 0.00s system 0% cpu 10.011 total

The last number gives the total time as though it were recorded with a real life stopwatch (what you want 99% of the time). The other values are explained here.

muru
  • 197,895
  • 55
  • 485
  • 740
stevec
  • 133
0

In Nushell, the command is timeit instead of time.

> timeit java -jar benchmark.jar concurrency
[program output]
3min 42sec 994ms 803µs 978ns
jobukkit
  • 5,220
-2
  1. Open bashrc file

    gedit ~\.bashrc
    
  2. Find the following text:

    if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] :\[\033[01;34m\]\w\[\033[00m\]\$ ' 
    
  3. And replace with:

    if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\] [\d|\t]:\[\033[01;34m\]\w\[\033[00m\]\$ ' 
    
  4. Restart terminal to check.

Sample output in terminal

muru
  • 197,895
  • 55
  • 485
  • 740
  • 1
    This will show the time. It will neither build the difference to the last command, nor will it account for waiting to type and hit ENTER or typing the command. – user unknown Nov 15 '22 at 23:54