TrumanWong

time

Count the total time spent on a given command

Supplementary instructions

The time command is used to determine how long a given command needs to run. It's useful for testing the performance of your scripts and commands.

For example, if you have two different scripts doing the same job and you want to know which one performs better, you can use the Linux time command to determine the execution time of each script.

This command is a command within the shell and is also a software package. The description of the software package is in the lower part of this document

grammar

time <command>

Parameters

Command: Specify the command and its parameters that need to be run.

Example

When testing a program or comparing different algorithms, execution time is very important. A good algorithm should be the one that takes the shortest time. All UNIX-like systems include the time command, which can be used to count time consumption. For example:

$ time ls
anaconda-ks.cfg install.log install.log.syslog satools text

real 0m0.009s
user 0m0.002s
sys 0m0.007s

The output here will display different results depending on the distribution version used, for example:

#Bash
real 0m33.961s
user 0m0.340s
sys 0m0.940s

#Zsh
0.34s user 0.94s system 4% cpu 33.961 total

# GNU time (sh)
0.34user 0.94system 0:33.96elapsed 4%CPU (0avgtext+0avgdata 6060maxresident)k
0inputs+201456outputs (0major+315minor)pagefaults 0swaps

real or total or elapsed (wall clock time) refers to the time from the start to the end of the call. It refers to the time from the moment you press the Enter key to the moment the command completes. user - CPU time spent in user mode. system or sys - CPU time spent in kernel mode.

Software package

The next section is about the /usr/bin/time binary executable provided by the time package, rather than the shell's built-in time command.

Syntax of software package

Some shells (e.g. bash ) have a built-in time command that provides similar information about time and possibly other resource usage.

To access the real command, you may need to specify its pathname (similar to /usr/bin/time).

time [options] command [arguments...]

Software package command parameters

-f format, --format=format Specifies the output format, which may override the format specified in the environment variable TIME. -p, --portability Use portable output formats. -o file, --output=file Do not send the results to stderr, but overwrite the specified file. -a, --append (Used with -o.) Does not overwrite but append. -v, --verbose Produce very verbose output of all the information the program knows about. -q, --quiet Do not report abnormal program termination (when the command is terminated by a signal) or non-zero exit status.

Software package example

Use the -o option to write execution times to a file:

/usr/bin/time -o outfile.txt ls

Use the -a option to append information:

/usr/bin/time -a -o outfile.txt ls

Use the -f option to format the time output:

/usr/bin/time -f "time: %U" ls

Parameters after the -f option:

Parameters Description
%E real time, the display format is [hour:] minute: second
%U user time.
%S sys time.
%C Command name and command line parameters for timing.
%D Process non-shared data area, in KB.
%x Command exit status.
%k Number of signals received by the process.
%w The number of times the process was swapped out of main memory.
%Z The page size of the system. This is a system constant. The value of the constant in the system will be different.
%P The percentage of CPU time obtained by the process. This value is equal to the user+system time divided by the total running time.
%K The average total memory usage of the process (data+stack+text), the unit is KB.
%w The number of times the process actively performed context switches, such as waiting for I/O operations to complete.
%c The number of times the process was forced to context switch (due to time slice expiration).

References