Time (Unix): Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
imported>Jasper Deng
 
imported>Cimumetupp
 
Line 40: Line 40:
wikipedia.org mail is handled by 10 mx1001.wikimedia.org.
wikipedia.org mail is handled by 10 mx1001.wikimedia.org.
host wikipedia.org  0.04s user 0.02s system 7% cpu 0.780 total
host wikipedia.org  0.04s user 0.02s system 7% cpu 0.780 total
$
</syntaxhighlight>
A simple [[stopwatch]] ({{key|Ctrl|d}} to stop):
<syntaxhighlight lang="console">
$ time read
real    0m9.760s
user    0m0.000s
sys    0m0.000s
$
$
</syntaxhighlight>
</syntaxhighlight>
Line 57: Line 67:


==Implementations==
==Implementations==
Depending on the shell the {{mono|time}} command defaults to being treated as either a shell keyword, [[Shell builtin|builtin]] or binary executable. One can also force the use of  the binary by referencing the file directly, ie: '''/usr/bin/time''' anyCommmand.  To  determine the default, issue the command: <code>'''type time'''</code>.<ref>{{Cite web|title=Linux  Time Command |url=https://linuxize.com/post/linux-time-command/ |access-date=July 24, 2025 |website=Linuxize}}</ref>


===GNU time===
===GNU time===
Line 69: Line 80:


Format of the output for [[GNU]] time, can be adjusted using <code>'''TIME'''</code> environment variable, and it can include information other than the execution time (i.e. memory usage). This behavior is not available in general [[POSIX]]-compliant time, or when executing as <code>time -p</code>.
Format of the output for [[GNU]] time, can be adjusted using <code>'''TIME'''</code> environment variable, and it can include information other than the execution time (i.e. memory usage). This behavior is not available in general [[POSIX]]-compliant time, or when executing as <code>time -p</code>.
The binary version sends its output to [[stderr]] instead of [[stdout]] but this can be overridden by using the <code>-o ''filename''</code> or <code>--output ''filename''</code> flags.


Documentation of this {{mono|time}} can be usually accessed using <code>man 1 time</code>.
Documentation of this {{mono|time}} can be usually accessed using <code>man 1 time</code>.
Line 89: Line 102:
The reported time is a time used by both <code>'''seq'''</code> and <code>'''wc -l'''</code> added up. Format of the output can be adjusted using <code>'''TIMEFORMAT'''</code> variable.
The reported time is a time used by both <code>'''seq'''</code> and <code>'''wc -l'''</code> added up. Format of the output can be adjusted using <code>'''TIMEFORMAT'''</code> variable.


The {{mono|time}} is not a builtin, but a special keyword, and can't be treated as a function or command. It also ignores pipeline redirections (even when executed as <code>time -p</code>, unless entire Bash is run in "POSIX mode").
When the {{mono|time}} is treated as a special keyword, it also ignores pipeline redirections even when executed as <code>time -p</code>.  The work around is to enclose the command group in braces, ie: <code>{ time ''anyCommand''; } 2> timing.Output</code><ref>{{cite web|first=Burak |last=Gökmen| title=Redirecting the Output of the time Command| date=June 16, 2025| access-date=July 24, 2025| url=https://www.baeldung.com/linux/redirect-time-output| publisher=Baeldung}}</ref>  Alternatively one could set the bash shell to run in "POSIX mode" can force the shell to ignore the reserved keyword implementation if an optional flag follows the {{mono|time}} command.<ref>{{cite web | publisher=gnu.or | title=Bash POSIX Mode | date=July 26, 2025 | access-date=July 28, 2025 | url=https://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html#Bash-POSIX-Mode-1}}</ref>


Documentation of this {{mono|time}} can be accessed using <code>man 1 bash</code>, or within bash itself using <code>help time</code>.
Documentation of {{mono|time}} can be accessed using <code>man 1 bash</code>, or within bash itself using <code>help time</code>.


==See also==
==See also==
Line 100: Line 113:


==References==
==References==
{{Reflist}}
* {{man|cu|time|SUS|time a simple command}}
* {{man|cu|time|SUS|time a simple command}}
* {{man|1|time|Inferno}}
* {{man|1|time|Inferno}}

Latest revision as of 03:42, 22 November 2025

Template:Short description Script error: No such module "about".

Script error: No such module "Infobox".Template:Template other Script error: No such module "Check for unknown parameters".Script error: No such module "Check for conflicting parameters". In computing, time is a command in Unix and Unix-like operating systems. It is used to determine the duration of execution of a particular command.

Overview

time(1) can exist as a standalone program (such as GNU time) or as a shell builtin in most cases (e.g. in sh, bash, tcsh or in zsh).

User time vs system time

The total CPU time is the combination of the amount of time the CPU or CPUs spent performing some action for a program and the amount of time they spent performing system calls for the kernel on the program's behalf. When a program loops through an array, it is accumulating user CPU time. Conversely, when a program executes a system call such as exec or fork, it is accumulating system CPU time.

Real time vs CPU time

The term "real time" in this context refers to elapsed wall-clock time, like using a stop watch. The total CPU time (user time + sys time) may be more or less than that value. Because a program may spend some time waiting and not executing at all (whether in user mode or system mode) the real time may be greater than the total CPU time. Because a program may fork children whose CPU times (both user and sys) are added to the values reported by the time command, but on a multicore system these tasks are run in parallel, the total CPU time may be greater than the real time.

Usage

To use the command, one simply precedes any command by the word time, such as:

$ time ls

When the command completes, time will report how long it took to execute the ls command in terms of user CPU time, system CPU time, and real time. The output format varies between different versions of the command, and some give additional statistics, as in this example:

$ time host wikipedia.org
wikipedia.org has address 103.102.166.224
wikipedia.org mail is handled by 50 mx2001.wikimedia.org.
wikipedia.org mail is handled by 10 mx1001.wikimedia.org.
host wikipedia.org  0.04s user 0.02s system 7% cpu 0.780 total
$

A simple stopwatch (Template:Key to stop):

$ time read

real    0m9.760s
user    0m0.000s
sys     0m0.000s
$

<templatestyles src="Mono/styles.css" />time (either a standalone program, or when Bash shell is running in POSIX mode AND <templatestyles src="Mono/styles.css" />time is invoked as time -p) reports to standard error output.

time -p

Portable scripts should use time -p mode, which uses a different output format, but which is consistent with various implementations:

$ time -p sha256sum /bin/ls
12477deb0e25209768cbd79328f943a7ea8533ece70256cdea96fae0ae34d1cc  /bin/ls
real 0.00
user 0.00
sys 0.00
$

Implementations

Depending on the shell the <templatestyles src="Mono/styles.css" />time command defaults to being treated as either a shell keyword, builtin or binary executable. One can also force the use of the binary by referencing the file directly, ie: /usr/bin/time anyCommmand. To determine the default, issue the command: type time.[1]

GNU time

Current versions of GNU time, report more than just a time by default:

$ /usr/bin/time sha256sum /bin/ls
12477deb0e25209768cbd79328f943a7ea8533ece70256cdea96fae0ae34d1cc  /bin/ls
0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 2156maxresident)k
0inputs+0outputs (0major+96minor)pagefaults 0swaps
$

Format of the output for GNU time, can be adjusted using TIME environment variable, and it can include information other than the execution time (i.e. memory usage). This behavior is not available in general POSIX-compliant time, or when executing as time -p.

The binary version sends its output to stderr instead of stdout but this can be overridden by using the -o filename or --output filename flags.

Documentation of this <templatestyles src="Mono/styles.css" />time can be usually accessed using man 1 time.

Method of operation

According to the source code of the GNU implementation of time, most information shown by time is derived from the wait3 system call. On systems that do not have a wait3 call that returns status information, the times system call is used instead.

Bash

In a popular Unix shell Bash, time is a special keyword, that can be put before a pipeline (or single command), that measures time of entire pipeline, not just a singular (first) command, and uses a different default format, and puts empty line before reporting times:

$ time seq 10000000 | wc -l
10000000

real	0m0.078s
user	0m0.116s
sys	0m0.029s
$

The reported time is a time used by both seq and wc -l added up. Format of the output can be adjusted using TIMEFORMAT variable.

When the <templatestyles src="Mono/styles.css" />time is treated as a special keyword, it also ignores pipeline redirections even when executed as time -p. The work around is to enclose the command group in braces, ie: { time anyCommand; } 2> timing.Output[2] Alternatively one could set the bash shell to run in "POSIX mode" can force the shell to ignore the reserved keyword implementation if an optional flag follows the <templatestyles src="Mono/styles.css" />time command.[3]

Documentation of <templatestyles src="Mono/styles.css" />time can be accessed using man 1 bash, or within bash itself using help time.

See also

Template:Sister project

References

<templatestyles src="Reflist/styles.css" />

  1. Script error: No such module "citation/CS1".
  2. Script error: No such module "citation/CS1".
  3. Script error: No such module "citation/CS1".

Script error: No such module "Check for unknown parameters".

Script error: No such module "Navbox".