##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# clock

From cppreference.com
< c‎ | chrono

Defined in header `<time.h>` |  |
clock_t clock(); |  |

Returns the approximate processor time used by the process since the beginning of an implementation-defined era related to the program's execution. To convert result value to seconds divide it by CLOCKS_PER_SEC.

Only the difference between two values returned by different calls to clock is meaningful, as the beginning of the clock era does not have to coincide with the start of the program. clock time may advance faster or slower than the wall clock, depending on the execution resources given to the program by the operating system. For example, if the CPU is shared by other processes, clock time may advance slower than wall clock. On the other hand, if the current process is multithreaded and more than one execution core is available, clock time may advance faster than wall clock.

### [edit] Parameters

(none)

### [edit] Return value

Processor time used by the program so far or (clock_t)(-1) if that information is unavailable.

### [edit] Notes

On POSIX-compatible systems, `clock_gettime` with clock id CLOCK_PROCESS_CPUTIME_ID offers better resolution.

The value returned by `clock()` may wrap around on some implementations. For example, on a machine with 32-bit clock_t, it wraps after 2147 seconds or 36 minutes.

### [edit] Example

This example demonstrates the difference between clock() time and real time

Run this code

    #include <math.h>
    #include <stdio.h>
    #include <time.h>

    // the function f() does some time-consuming work
    void f()
    {
        volatile double d; // we dont want subsequent computation to be optimized out
        for (int n=0; n<10000; ++n)
           for (int m=0; m<10000; ++m)
               d += d*n*m;
    }

    int main ()
    {
        clock_t t = clock();
        f();
        t = clock() - t;
        printf("Time used: %f seconds\n",((float)t)/CLOCKS_PER_SEC);

        return 0;
    }

Output:

    Time used: 1.010000 seconds

### [edit] See also

ctime | converts a `time_t` object to a textual representation (function) [edit]
time | returns the current time of the system as time since epoch (function) [edit]
C++ documentation for clock

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/chrono/clock&oldid=66469"