| Functions | ||||
| Time manipulation | ||||
clock | ||||
(C11) | ||||
(C23) | ||||
| Format conversions | ||||
(deprecated in C23)(C11) | ||||
(deprecated in C23)(C11) | ||||
(C95) | ||||
(C23)(C11) | ||||
(C23)(C11) | ||||
| Constants | ||||
| Types | ||||
(C11) |
Defined in header <time.h> | ||
clock_t clock(void); | ||
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 byCLOCKS_PER_SEC.
Only the difference between two values returned by different calls toclock is meaningful, as the beginning of theclock 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.
Contents |
Processor time used by the program so far.
On POSIX-compatible systems,clock_gettime with clock idCLOCK_PROCESS_CPUTIME_ID offers better resolution.
The value returned byclock() may wrap around on some implementations. For example, on such an implementation, ifclock_t is a signed 32-bit integer andCLOCKS_PER_SEC is1000000, it will wrap after about 2147 seconds (about 36 minutes).
This example demonstrates the difference betweenclock() time and real time.
#ifndef __STDC_NO_THREADS__#include <threads.h>#else// POSIX alternative#define _POSIX_C_SOURCE 199309L#include <pthread.h>#endif #include <stdio.h>#include <stdlib.h>#include <time.h> // the function f() does some time-consuming workint f(void* thr_data)// return void* in POSIX{(void) thr_data;volatiledouble d=0;for(int n=0; n<10000;++n)for(int m=0; m<10000;++m) d+= d* n* m;return0;} int main(void){struct timespec ts1, tw1;// both C11 and POSIX clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&ts1);// POSIX clock_gettime(CLOCK_MONOTONIC,&tw1);// POSIX; use timespec_get in C11clock_t t1= clock(); #ifndef __STDC_NO_THREADS__thrd_t thr1, thr2;// C11; use pthread_t in POSIXthrd_create(&thr1, f,NULL);// C11; use pthread_create in POSIXthrd_create(&thr2, f,NULL);thrd_join(thr1,NULL);// C11; use pthread_join in POSIXthrd_join(thr2,NULL);#endif struct timespec ts2, tw2; clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&ts2); clock_gettime(CLOCK_MONOTONIC,&tw2);clock_t t2= clock(); double dur=1000.0*(t2- t1)/CLOCKS_PER_SEC;double posix_dur=1000.0* ts2.tv_sec+1e-6* ts2.tv_nsec-(1000.0* ts1.tv_sec+1e-6* ts1.tv_nsec);double posix_wall=1000.0* tw2.tv_sec+1e-6* tw2.tv_nsec-(1000.0* tw1.tv_sec+1e-6* tw1.tv_nsec); printf("CPU time used (per clock()): %.2f ms\n", dur);printf("CPU time used (per clock_gettime()): %.2f ms\n", posix_dur);printf("Wall time passed: %.2f ms\n", posix_wall);}
Possible output:
CPU time used (per clock()): 1580.00 msCPU time used (per clock_gettime()): 1582.76 msWall time passed: 792.13 ms
(deprecated in C23)(C11) | converts atime_t object to a textual representation (function)[edit] |
| returns the current calendar time of the system as time since epoch (function)[edit] | |
C++ documentation forclock | |