How to determine process execution time

Use GNU Bourne-Again SHell builtin or standalone time command to determine process execution time.

GNU Bourne-Again SHell builtin time command

Use Bash builtin time command to get real execution time.

$ time rsync --dry-run -Havip --delete /home/milosz/docs/ /backup/notebook-docs/
.d..t...... project-x/.git/
>f.st...... project-x/.git/COMMIT_EDITMSG
>f.st.o.... project-x/.git/index
>f.st...... project-x/.git/logs/HEAD
>f.st...... project-x/.git/logs/refs/heads/master
>f.st...... project-x/.git/logs/refs/remotes/origin/master
.d..t...... project-x/.git/modules/core/
.d..t...... project-x/.git/objects/06/
>f+++++++++ project-x/.git/objects/05/253ae0470d4b948c40d83df9171b0f282bea1d
.d..t...... project-x/.git/objects/35/
>f+++++++++ project-x/.git/objects/35/a62b543d2865dadc0dafebd2bf2d8bf78eff6d
.d..t...... project-x/.git/objects/36/
>f+++++++++ project-x/.git/objects/36/dac5f479c331e66bb7297c3253dc3c3f6724e0
.d..t...... project-x/.git/objects/3a/
>f+++++++++ project-x/.git/objects/3a/1f44736cd56a68e8db2e29aa14cba5379b2e85
.d..t...... project-x/.git/objects/93/
>f+++++++++ project-x/.git/objects/93/f049ceae4d2ec503143a534ee3754b4dd1cb46
.d..t...... project-x/.git/objects/a4/
>f+++++++++ project-x/.git/objects/a4/e9fb24ab0415c57cd2fa3751dfa32efaecd9a9
.d..t...... project-x/.git/objects/d9/
>f+++++++++ project-x/.git/objects/d9/56475059256c53b1c8bcf22442453d7de02c7e
.d..t...... project-x/.git/refs/heads/
>f..t.o.... project-x/.git/refs/heads/master
.d..t...... project-x/.git/refs/remotes/origin/
>f..t.o.... project-x/.git/refs/remotes/origin/master
.d..t...... project-x/.git/refs/tags/
>f+++++++++ project-x/.git/refs/tags/1.1

real    0m29.795s
user    0m12.692s
sys     0m29.624s

You can define TIMEFORMAT variable to modify output format.

$ export TIMEFORMAT="Execution time: %0lR (%0R seconds)"
$ time rsync --dry-run -Havip --delete /home/milosz/docs/ /backup/notebook-docs/
[...]
Execution time: 0m31s (31 seconds)

Inspect bash manual page for detailed TIMEFORMAT parameter description.

Standalone GNU time command

Install dedicated GNU time program for measuring CPU resource usage.

$ sudo apt-get install time
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  time
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 31.6 kB of archives.
After this operation, 84.0 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian stretch/main amd64 time amd64 1.7-25.1+b1 [31.6 kB]
Fetched 31.6 kB in 0s (1760 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package time.
(Reading database ... 8412 files and directories currently installed.)
Preparing to unpack .../time_1.7-25.1+b1_amd64.deb ...
Unpacking time (1.7-25.1+b1) ...
Setting up time (1.7-25.1+b1) ...

Display portable time format string for conformance with POSIX standard 1003.2.

$ /usr/bin/time --portability rsync --dry-run -Havip --delete /home/milosz/docs/ /backup/notebook-docs/
.d..t...... project-x/.git/
>f.st...... project-x/.git/COMMIT_EDITMSG
>f.st.o.... project-x/.git/index
>f.st...... project-x/.git/logs/HEAD
>f.st...... project-x/.git/logs/refs/heads/master
>f.st...... project-x/.git/logs/refs/remotes/origin/master
.d..t...... project-x/.git/modules/core/
.d..t...... project-x/.git/objects/06/
>f+++++++++ project-x/.git/objects/05/253ae0470d4b948c40d83df9171b0f282bea1d
.d..t...... project-x/.git/objects/35/
>f+++++++++ project-x/.git/objects/35/a62b543d2865dadc0dafebd2bf2d8bf78eff6d
.d..t...... project-x/.git/objects/36/
>f+++++++++ project-x/.git/objects/36/dac5f479c331e66bb7297c3253dc3c3f6724e0
.d..t...... project-x/.git/objects/3a/
>f+++++++++ project-x/.git/objects/3a/1f44736cd56a68e8db2e29aa14cba5379b2e85
.d..t...... project-x/.git/objects/93/
>f+++++++++ project-x/.git/objects/93/f049ceae4d2ec503143a534ee3754b4dd1cb46
.d..t...... project-x/.git/objects/a4/
>f+++++++++ project-x/.git/objects/a4/e9fb24ab0415c57cd2fa3751dfa32efaecd9a9
.d..t...... project-x/.git/objects/d9/
>f+++++++++ project-x/.git/objects/d9/56475059256c53b1c8bcf22442453d7de02c7e
.d..t...... project-x/.git/refs/heads/
>f..t.o.... project-x/.git/refs/heads/master
.d..t...... project-x/.git/refs/remotes/origin/
>f..t.o.... project-x/.git/refs/remotes/origin/master
.d..t...... project-x/.git/refs/tags/
>f+++++++++ project-x/.git/refs/tags/1.1

real 28.88
user 12.56
sys 28.76

Display custom time format defined using format option.

$ /usr/bin/time --format="Execution time: %E (%e seconds)" rsync --dry-run -Havip --delete /home/milosz/docs/ /backup/notebook-docs/
[...]
Execution time: 0:29.29 (29.29 seconds)

Display custom time format defined using TIME variable.

$ export TIME="Execution time: %E (%e seconds)"
$ /usr/bin/time rsync --dry-run -Havip --delete /home/milosz/docs /backup/notebook-docs
[...]
Execution time: 0:29.99 (29.99 seconds)

Execute command five times in a row and append custom time format defined using format option to the specified file.

$ for i in $(seq 1 5); do \
  /usr/bin/time --append --output rsync-time.log \
                --format="Execution time: %E (%e seconds)" \
                rsync --dry-run -Havip --delete /home/milosz/docs/ /backup/notebook-docs/; \
  done
[...]
$ cat rsync-time.log
Execution time: 0:27.50 (27.50 seconds)
Execution time: 0:28.57 (28.57 seconds)
Execution time: 0:29.31 (29.31 seconds)
Execution time: 0:29.64 (29.64 seconds)
Execution time: 0:30.44 (30.44 seconds)

Inspect time manual page for detailed output format description.

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.