HOME

TheInfoList



OR:

CPU time (or process time) is the amount of
time Time is the continuous progression of existence that occurs in an apparently irreversible process, irreversible succession from the past, through the present, and into the future. It is a component quantity of various measurements used to sequ ...
that a
central processing unit A central processing unit (CPU), also called a central processor, main processor, or just processor, is the primary Processor (computing), processor in a given computer. Its electronic circuitry executes Instruction (computing), instructions ...
(CPU) was used for processing instructions of a
computer program A computer program is a sequence or set of instructions in a programming language for a computer to Execution (computing), execute. It is one component of software, which also includes software documentation, documentation and other intangibl ...
or
operating system An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs. Time-sharing operating systems scheduler (computing), schedule tasks for ...
. CPU time is measured in clock ticks or seconds. Sometimes it is useful to convert CPU time into a percentage of the CPU capacity, giving the CPU usage. Measuring CPU time for two functionally identical programs that process identical inputs can indicate which program is faster, but it is a common misunderstanding that CPU time can be used to compare ''algorithms''. Comparing programs by their CPU time compares specific ''implementations'' of algorithms. (It is possible to have both efficient and inefficient implementations of the same algorithm.) Algorithms are more commonly compared using measures of
time complexity In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations ...
and space complexity. Typically, the CPU time used by a program is measured by the operating system, which schedules all of the work of the CPU. Modern multitasking operating systems run hundreds of processes. (A
process A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management * Business process, activities that produce a specific s ...
is a running program.) Upon starting a process, the operating system records the time using an internal timer. When the process is suspended or terminated, the operating system again records the time. The total time that a process spent running is its CPU time, as shown in the figure.


User and System time

The process "accounting" done by the Unix family of operating systems includes two components of CPU time. User time and System time reflect the fact that most programs make requests to the operating system while they execute.
Input/output In computing, input/output (I/O, i/o, or informally io or IO) is the communication between an information processing system, such as a computer, and the outside world, such as another computer system, peripherals, or a human operator. Inputs a ...
operations, such as reading a file or writing to the screen, are done by issuing requests to the operating system, possibly through system calls. I/O and other operations performed by the operating system on behalf of a process constitute system time. * User time is the amount of time the CPU is busy executing code in
user space A modern computer operating system usually uses virtual memory to provide separate address spaces or regions of a single address space, called user space and kernel space. This separation primarily provides memory protection and hardware prote ...
. * System time is the amount of time the CPU is busy executing code in
kernel space A modern computer operating system usually uses virtual memory to provide separate address spaces or regions of a single address space, called user space and kernel space. This separation primarily provides memory protection and hardware prote ...
. This value represents the amount of time the kernel performs work on behalf of the executing process. In contrast, elapsed real time (or simply real time, or wall-clock time) is the time taken from the start of a computer program until the end as measured by an ordinary clock. Elapsed real time includes User time, System time, plus time that the process was not running for any reason, such as when its execution was preempted.


Unix commands for CPU time


Unix command ''top''

The
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user 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, a ...
command
top Top most commonly refers to: * Top, a basic term of orientation, distinguished from bottom, front, back, and sides * Spinning top, a ubiquitous traditional toy * Top (clothing), clothing designed to be worn over the torso * Mountain top, a moun ...
provides CPU time, priority, elapsed real time, and other information for all processes and updates it in real time.


Unix command ''time''

The
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user 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, a ...
command
time Time is the continuous progression of existence that occurs in an apparently irreversible process, irreversible succession from the past, through the present, and into the future. It is a component quantity of various measurements used to sequ ...
prints CPU time and elapsed real time for the execution of a Unix command (or
pipeline A pipeline is a system of Pipe (fluid conveyance), pipes for long-distance transportation of a liquid or gas, typically to a market area for consumption. The latest data from 2014 gives a total of slightly less than of pipeline in 120 countries ...
). Note that many command-line shells have their own implementation of this command. To run the Unix program time, we provide its full path, /usr/bin/time: $ gcc nextPrimeNumber.c -o nextPrimeNumber -lm $ /usr/bin/time ./nextPrimeNumber 300000070034 Prime number greater than 300000070034 is 300000070043 0.01user 0.00system 0:00.01elapsed 100%CPU $ This process took a total of 0.02 seconds of CPU time (User + System). The reported System time is 0.00 seconds, indicating that the amount of System time used was less than the printed resolution of 0.01 seconds. Elapsed real time was 0.08 seconds. The following is the source code of the application nextPrimeNumber which was used in the above example. // nextPrimeNumber.c #include #include #include int isPrimeNumber(unsigned long int n) int main(int argc, char *argv[])


Functions to get CPU time

Modern CPUs have several clocks and counters, such as the Time Stamp Counter, the High Precision Event Timer, and the Real-time clock , Real-time Clock, each with a specialized use. When a program wants to time its own operation, it can use a function like the C POSIX library, POSIX clock() function, which returns the CPU time used by the program. POSIX allows this clock to start at an arbitrary value, so to measure elapsed time, a program calls clock(), does some work, then calls clock() again.See, for example, https://www.gnu.org/software/libc/manual/html_node/CPU-Time.html The difference is the time needed to do the work. The POSIX function getrusage() returns more than just the CPU time consumed by a process in a POSIX environment. It returns many measurements of a process, often including approximate memory usage and Context switch (scheduling) event counts. Functionality varies across operating systems.


Total CPU time

On multi-processor and
multi-core A multi-core processor (MCP) is a microprocessor on a single integrated circuit (IC) with two or more separate central processing units (CPUs), called ''cores'' to emphasize their multiplicity (for example, ''dual-core'' or ''quad-core''). Ea ...
machines, a program can use two or more processors simultaneously in what is called parallel processing. In such situations, a measure of ''total CPU time'' is useful, which is the sum of CPU time consumed by all of the processors utilized by the program.


CPU time and elapsed real time

Elapsed real time is always greater than or equal to the CPU time for computer programs which use only one CPU for processing. If no waiting occurs, such as for I/O, and the program's execution is never preempted, elapsed real time and CPU time will be virtually identical.


CPU time and elapsed real time for parallel processing

If a program uses parallel processing, total CPU time for that program is typically more than its elapsed real time. For a program that is able to evenly divide its work across two processors with no overhead in doing so, the value (Total CPU time)/(Number of processors) will be virtually identical to elapsed real time. Here, a ''processor'' may be a (single-core) CPU or one core in a multi-core CPU. Example: A software application executed on a four-core processor creates four Unix processes. If each process is able to execute on a separate processor core, computation proceeds on four processor cores simultaneously. The total CPU time would be, ideally, four times the elapsed real time. In reality, parallel processing rarely achieves a linear speedup, where the amount of computation per unit time scales up with the number of processors in use. Some embarrassingly parallel problems admit such solutions, but for most, additional work is required to divide up the computation when the program starts, and to combine the results from each processor at the end. The additional work adds to the total CPU time. Frequently, a process finds itself waiting for data from another process before it can continue, which also adds to the total time.


See also

* Elapsed real time * CPU *
Process (computing) In computing, a process is the Instance (computer science), instance of a computer program that is being executed by one or many thread (computing), threads. There are many different process models, some of which are light weight, but almost all ...
*
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 calendar date, days on the calendar. System time is measured by a ''sys ...
* Unix top program *
Load (computing) In UNIX computing, the system load is a measure of the amount of computational work that a computer system performs. The load average represents the average system load over a period of time. It conventionally appears in the form of three numb ...


References


External links

* * * * {{cite web , url=https://www.opengroup.org/onlinepubs/000095399/functions/getrusage.html , title=getrusage - get information about resource utilization , work=The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition , accessdate=2014-08-05 Computer performance