CPU time

Source: Wikipedia, the free encyclopedia.
CPU Time on Single CPU Multi Tasking System
  CPU color time for program P1

CPU time (or process time) is the amount of

clock ticks
or seconds. Often, it is useful to measure CPU time as a percentage of the CPU's capacity, which is called the CPU usage. CPU time and CPU usage have two main uses.

The CPU time is used to quantify the overall empirical efficiency of two functionally identical algorithms. For example any

running time complexity
such that merge sort tends to complete in fewer steps. Without any knowledge of the workings of either algorithm a greater CPU time of bubble sort shows it is less efficient for particular input data than merge sort.

This type of measurement is especially useful when comparing like algorithms that are not trivial in complexity. In this case the wall time (actual duration elapsed) is irrelevant, the computer may execute the program slower or faster depending on real world variables such as the CPU's temperature, as well as other operating system variables, such as the process's priority.

The CPU usage is used to quantify how the processor is shared between computer programs. High CPU usage by a single program may indicate that it is highly demanding of processing power or that it may malfunction; for example, it has entered an infinite loop. CPU time allows measurement of the processing power a single program requires, eliminating interference, such as time executed waiting for input or being suspended to allow other programs to run.

In contrast,

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 I/O time, any multitasking delays, and all other types of waits incurred by the program.

Subdivision

CPU time or CPU usage can be reported either for each thread, for each process or for the entire system. Moreover, depending on what exactly the CPU was doing, the reported values can be subdivided in:

Unix commands for CPU time

top's display of the CPU time of various processes on a Unix-like (GNU/Linux) system

Unix command top

The Unix command top provides CPU time, priority, elapsed real time, and other information for all processes and updates it in real time.

Unix command time

The Unix command time prints CPU time and elapsed real time for a Unix process.

% gcc nextPrimeNumber.c -o nextPrimeNumber
% time ./nextPrimeNumber 30000007
Prime number greater than 30000007 is 30000023
0.327u 0.010s 0:01.15 28.6%     0+0k 0+0io 0pf+0w

This process took a total of 0.337 seconds of CPU time, out of which 0.327 seconds was spent in user space, and the final 0.010 seconds in kernel mode on behalf of the process. Elapsed real time was 1.15 seconds.

The following is the source code of the application nextPrimeNumber which was used in the above example.

// nextPrimeNumber.c
#include <stdio.h>
#include <stdlib.h>

int isPrimeNumber(unsigned long int n) {
    for (int i = 2; i <= (n >> 1); ++i)
        if (n % i == 0) return 0;
    return 1;
}

int main(int argc, char *argv[]) {
    unsigned long int argument = strtoul(argv[1], NULL, 10), n = argument;
    while (!isPrimeNumber(++n));

    printf("Prime number greater than %lu is %lu\n", argument, n);
    return 0;
}

POSIX functions clock() and getrusage()

threads
. With Linux starting from kernel 2.6.26 there is a parameter RUSAGE_THREAD which leads to resource usage statistics for the calling thread only.

Total CPU time

On

CPUs for processing using parallel processing
scheduling. In such situations, the notion of total CPU time is used, which is the sum of CPU time consumed by all of the CPUs utilized by the computer 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 wait is involved for I/O or other resources, elapsed real time and CPU time are very similar.

CPU time and elapsed real time for parallel processing technology

If a program uses parallel processing, total CPU time for that program would be more than its elapsed real time. (Total CPU time)/(Number of CPUs) would be same as elapsed real time if the work load is evenly distributed on each CPU and no wait is involved for I/O or other resources.

Example: A software application executed on a hexa-core processor creates three Unix processes for fulfilling the user requirement. Each of these three processes creates two threads, enumerating a total of 6 working threads. Computation is distributed evenly on the 6 independent threads. If no wait for resources is involved, total CPU time is expected to be six times the elapsed real time.

See also

References

  1. ^ Ehrhardt, Christian (July 2010). "CPU time accounting". IBM. Retrieved 2014-08-05.

External links