cputime

CPU time used by the session, in seconds.

Category: Software Development Tools > Performance and Memory

Syntax

t = cputime

Returns the total CPU time consumed so far.

Description

The value counts processor time, not wall-clock time: a program that sleeps or waits adds almost nothing, while one that computes continuously adds about one second per second of work. Only differences are meaningful, so read cputime before and after a block and subtract.

On machines with several cores the value can grow faster than real time. For measuring how long code takes to run, tic and toc or timeit are usually a better fit.

Output arguments

t
Scalar double with the accumulated CPU seconds.

Example

Difference between two readings

t0 = cputime;
s = 0;
for k = 1:1e5
    s = s + k;
end
dt = cputime - t0;
disp(dt >= 0)
disp(s)
Output
   1
   5.0000e+09

See also

tic toc timeit clock now