Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

time (Unix)

From Wikipedia, the free encyclopedia
Command in Unix and Unix-like operating systems
This article is about the Unix command. For Unix's means of representing points in time, seeUnix time. For the Unix function call, seetime.h.
time
Example oftime command
DevelopersVariousopen-source andcommercial developers
Operating systemUnix,Unix-like,Inferno
PlatformCross-platform
TypeCommand

Incomputing,time is a command inUnix andUnix-like operating systems. It is used to determine the duration of execution of a particularcommand.

Overview

[edit]

time(1) can exist as a standalone program (such asGNU time) or as ashell builtin in most cases (e.g. insh,bash,tcsh or inzsh).

User time vs system time

[edit]

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 performingsystem calls for thekernel on the program's behalf. When a program loops through an array, it is accumulating user CPU time. Conversely, when a program executes asystem call such asexec orfork, it is accumulating system CPU time.

Real time vs CPU time

[edit]

The term "real time" in this context refers to elapsedwall-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 thetime command, but on a multicore system these tasks are run in parallel, the total CPU time may be greater than the real time.

Usage

[edit]

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

$timels

When the command completes,time will report how long it took to execute thels command in terms of userCPU 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:

$timehostwikipedia.orgwikipedia.org has address 103.102.166.224wikipedia.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 simplestopwatch (Ctrl+d to stop):

$timereadreal    0m9.760suser    0m0.000ssys     0m0.000s$

time (either a standalone program, or when Bash shell is running in POSIX mode ANDtime is invoked astime -p) reports to standard error output.

time -p

[edit]

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

$time-psha256sum/bin/ls12477deb0e25209768cbd79328f943a7ea8533ece70256cdea96fae0ae34d1cc  /bin/lsreal 0.00user 0.00sys 0.00$

Implementations

[edit]

Depending on the shell thetime 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

[edit]

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

$/usr/bin/timesha256sum/bin/ls12477deb0e25209768cbd79328f943a7ea8533ece70256cdea96fae0ae34d1cc  /bin/ls0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 2156maxresident)k0inputs+0outputs (0major+96minor)pagefaults 0swaps$

Format of the output forGNU time, can be adjusted usingTIME environment variable, and it can include information other than the execution time (i.e. memory usage). This behavior is not available in generalPOSIX-compliant time, or when executing astime -p.

The binary version sends its output tostderr instead ofstdout but this can be overridden by using the-ofilename or--outputfilename flags.

Documentation of thistime can be usually accessed usingman 1 time.

Method of operation

[edit]

According to the source code of the GNU implementation oftime, most information shown bytime is derived from thewait3 system call. On systems that do not have await3 call that returns status information, thetimes system call is used instead.

Bash

[edit]

In a popular Unix shellBash,time is a special keyword, that can be put before apipeline (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:

$timeseq10000000|wc-l10000000real0m0.078suser0m0.116ssys0m0.029s$

The reported time is a time used by bothseq andwc -l added up. Format of the output can be adjusted usingTIMEFORMAT variable.

When thetime is treated as a special keyword, it also ignores pipeline redirections even when executed astime -p. The work around is to enclose the command group in braces, ie:{ timeanyCommand; } 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 thetime command.[3]

Documentation oftime can be accessed usingman 1 bash, or within bash itself usinghelp time.

See also

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands

References

[edit]
  1. ^"Linux Time Command".Linuxize.Archived from the original on July 22, 2025. RetrievedJuly 24, 2025.
  2. ^Gökmen, Burak (June 16, 2025)."Redirecting the Output of the time Command". Baeldung.Archived from the original on September 13, 2025. RetrievedJuly 24, 2025.
  3. ^"Bash POSIX Mode". gnu.or. July 26, 2025.Archived from the original on June 13, 2023. RetrievedJuly 28, 2025.
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
Retrieved from "https://en.wikipedia.org/w/index.php?title=Time_(Unix)&oldid=1336331262"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp