HOME

TheInfoList



OR:

In
computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes, and development of both hardware and software. Computing has scientific, e ...
, time is a command in
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and ot ...
and
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
operating systems. It is used to determine the duration of execution of a particular
command Command may refer to: Computing * Command (computing), a statement in a computer language * COMMAND.COM, the default operating system shell and command-line interpreter for DOS * Command key, a modifier key on Apple Macintosh computer keyboards * ...
.


Overview

time(1) can exist as a standalone program (such as
GNU GNU () is an extensive collection of free software (383 packages as of January 2022), which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operat ...
time) or as a
shell Shell may refer to: Architecture and design * Shell (structure), a thin structure ** Concrete shell, a thin shell of concrete, usually with no interior columns or exterior buttresses ** Thin-shell structure Science Biology * Seashell, a hard ou ...
builtin in most case (e.g. in sh,
bash Bash or BASH may refer to: Arts and entertainment * ''Bash!'' (Rockapella album), 1992 * ''Bash!'' (Dave Bailey album), 1961 * '' Bash: Latter-Day Plays'', a dramatic triptych * ''BASH!'' (role-playing game), a 2005 superhero game * "Bash" ('' ...
,
tcsh tcsh ( “tee-see-shell”, “tee-shell”, or as “tee see ess aitch”, tcsh) is a Unix shell based on and backward compatible with the C shell (csh). Shell It is essentially the C shell with programmable command-line completion, command-l ...
or in
zsh The Z shell (Zsh) is a Unix shell that can be used as an interactive login shell (computing), shell and as a command line interpreter, command interpreter for shell scripting. Zsh is an extended Bourne shell with many improvements, including som ...
).


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 call In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
s for the
kernel Kernel may refer to: Computing * Kernel (operating system), the central component of most operating systems * Kernel (image processing), a matrix used for image convolution * Compute kernel, in GPGPU programming * Kernel method, in machine learnin ...
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 In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
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 Elapsed real time, real time, wall-clock time, wall time, or walltime is the actual time taken from the start of a computer program to the end. In other words, it is the difference between the time at which a task finishes and the time at which the ...
, 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, simply precede 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 CPU time (or process time) is the amount of time for which a central processing unit (CPU) was used for processing instructions of a computer program or operating system, as opposed to elapsed time, which includes for example, waiting for input ...
, 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 $ (either a standalone program, or when Bash shell is running in POSIX mode AND 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


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 GNU () is an extensive collection of free software (383 packages as of January 2022), which can be used as an operating system or can be used in parts with other operating systems. The use of the completed GNU tools led to the family of operat ...
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 The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines both the system- and user-level application programming interf ...
-compliant time, or when executing as time -p. Documentation of this 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 Bash or BASH may refer to: Arts and entertainment * ''Bash!'' (Rockapella album), 1992 * ''Bash!'' (Dave Bailey album), 1961 * '' Bash: Latter-Day Plays'', a dramatic triptych * ''BASH!'' (role-playing game), a 2005 superhero game * "Bash" ('' ...
, time is a special keyword, that can be put before a
pipeline Pipeline may refer to: Electronics, computers and computing * Pipeline (computing), a chain of data-processing stages or a CPU optimization found on ** Instruction pipelining, a technique for implementing instruction-level parallelism within a s ...
(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. The 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 time -p, unless entire Bash is run in "POSIX mode"). Documentation of this can be accessed using man 1 bash, or within bash itself using help time.


See also

*
System time In computer science and computer programming, system time represents a computer system's notion of the passage of time. In this sense, ''time'' also includes the passing of days on the calendar. System time is measured by a ''system clock'', w ...
*
Cron The cron command-line utility is a job scheduler on Unix-like operating systems. Users who set up and maintain software environments use cron to schedule jobs (commands or shell scripts), also known as cron jobs, to run periodically at fixed ti ...
process for scheduling jobs to run at a particular time *
TIME (command) In computing, TIME is a command in DEC RT-11, DOS, IBM OS/2, Microsoft Windows and a number of other operating systems that is used to display and set the current system time. It is included in command-line interpreters ( shells) such as COMMAND ...


References

* * * {{Unix commands Unix SUS2008 utilities Unix process- and task-management-related software Inferno (operating system) commands