The C clock() function reports an implementation-defined count related to processor time consumed by the program. It is useful for measuring CPU work, not for reading the current date or wall-clock time.
Subtract two clock_t readings and divide by CLOCKS_PER_SEC to obtain seconds as a floating-point value. On failure, the function returns (clock_t)-1. Include <time.h>.
Syntax
Start with this concrete clock() call:
C
clock();
Call
What it does
clock();
Reads the program's current processor-time counter.
Declare the required objects with the time types shown below, include <time.h>, and check the documented result before using output data.
Library declaration
C
clock_t clock(void);
Parameters
Parameter
Description
None
clock() takes no arguments.
Return Value
Return Value
Description
clock_t count
Current processor-time count for the program.
(clock_t)-1
Processor time is unavailable or cannot be represented.
C Examples
Example 1 — Measure a Loop
This example demonstrates clock() with a small, complete program and checks the relevant result before using it.
C
#include <stdio.h>
#include <time.h>
int main(void) {
clock_t start = clock();
volatile long total = 0;
for (long i = 0; i < 1000000; ++i) total += i;
clock_t finish = clock();
double seconds = (double)(finish - start) / CLOCKS_PER_SEC;
printf("CPU seconds: %.6f\n", seconds);
return 0;
}
Output
CPU seconds: 0.002431
(example value varies)
How It Works
Step 1. Read the processor counter before the loop.
Step 2. Run a small CPU-bound calculation.
Step 3. Subtract the first count from the second.
Step 4. Divide by CLOCKS_PER_SEC; the exact result depends on the machine.
Example 2 — Check for Failure
This example demonstrates clock() with a small, complete program and checks the relevant result before using it.
C
#include <stdio.h>
#include <time.h>
int main(void) {
clock_t value = clock();
if (value == (clock_t)-1) {
printf("CPU time unavailable\n");
} else {
printf("clock ticks: %lld\n", (long long)value);
}
return 0;
}
Output
clock ticks: 15625
(example value varies)
How It Works
Step 1. Call clock() once.
Step 2. Compare with the documented failure sentinel.
Step 3. Convert to long long only for this simple display.
Step 4. A successful reading is an implementation-defined tick count, not seconds yet.
Example 3 — Compare Two Workloads
This example demonstrates clock() with a small, complete program and checks the relevant result before using it.
C
#include <stdio.h>
#include <time.h>
int main(void) {
clock_t start = clock();
for (volatile long i = 0; i < 500000; ++i) { }
clock_t middle = clock();
for (volatile long i = 0; i < 1000000; ++i) { }
clock_t end = clock();
printf("first: %.4f s\n", (double)(middle-start)/CLOCKS_PER_SEC);
printf("second: %.4f s\n", (double)(end-middle)/CLOCKS_PER_SEC);
return 0;
}
Output
first: 0.0011 s
second: 0.0022 s
(example values vary)
How It Works
Step 1. Capture readings around each separate loop.
Step 2. The first subtraction covers only the first workload.
Step 3. The second subtraction covers only the second workload.
Step 4. Convert both intervals with the same CLOCKS_PER_SEC constant.
Frequently Asked Questions
clock() measures processor time used by the program.
Include <time.h> before calling clock().
On success it returns the processor-time count. On failure it returns (clock_t)-1.
clock() is not a stopwatch for elapsed real time. Sleeping or waiting for input may add little or no processor time.
It is declared by the C time library; exact representations, locale, time-zone data, and resolution can still vary by implementation.