Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

clock — C23 processor-time function

`clock()` from `<time.h>` returns the best available approximation of processor time actively consumed by the program since an implementation-defined era related to its invocation. The result is `clock_t`; divide by `CLOCKS_PER_SEC` to obtain seconds.

Version: C23

Syntax

clock_t clock(void);

clock() from <time.h> returns the best available approximation of processor time actively consumed by the program since an implementation-defined era related to its invocation. The result is clock_t; divide by CLOCKS_PER_SEC to obtain seconds.

Use it to compare CPU consumption of computation regions. The example genuinely calls clock() and, in the verifier environment, prints 1 1 to confirm that a reading is available and that the CLOCKS_PER_SEC scale is positive.

It is not a wall clock, calendar clock, or guaranteed monotonic clock. Handle (clock_t)-1, measure a difference between two readings, allow for resolution and possible clock_t wraparound, and cast to double before division to avoid integer truncation.

Examples

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

int main(void) {
    clock_t stamp = clock();
    printf("%d %d\n", stamp != (clock_t)-1, CLOCKS_PER_SEC > 0);
    return 0;
}

Result

1 1

Use it to compare CPU consumption of computation regions. The example genuinely calls `clock()` and, in the verifier environment, prints `1 1` to confirm that a reading is available and that the `CLOCKS_PER_SEC` scale is positive.

Sources