time() / difftime() / clock()
Functions and types for retrieving the current time and measuring elapsed time. Defined in <time.h>, they are used for recording timestamps, measuring execution time, and performing date/time calculations.
Syntax
// Returns the current calendar time. // Return value: a time_t value representing the current time (seconds since the epoch). Returns (time_t)(-1) on failure. // If t is not NULL, the result is also stored in *t. time_t time(time_t *t); // Returns the difference between two time_t values in seconds. // Return value: the difference time1 - time0, as a double (in seconds). double difftime(time_t time1, time_t time0); // Returns the processor time used by the program. // Return value: a clock_t value. Divide by CLOCKS_PER_SEC to convert to seconds. clock_t clock(void);
Types and Constants
| Name | Kind | Description |
|---|---|---|
| time_t | Type | An arithmetic type representing calendar time. Typically holds the number of seconds since 00:00:00 UTC on January 1, 1970 (the Unix epoch). |
| clock_t | Type | An arithmetic type representing processor time. Divide the return value of clock() by CLOCKS_PER_SEC to convert to seconds. |
| CLOCKS_PER_SEC | Constant | A constant used to convert the return value of clock() to seconds (typically 1000000). |
| time() | Function | Returns the current calendar time (seconds since the epoch). |
| difftime() | Function | Returns the difference between two time_t values as a double, in seconds. |
| clock() | Function | Returns the processor time used since the program started. |
Sample Code
#include <stdio.h>
#include <time.h>
// Simulates a heavy workload (replace with meaningful work in real code).
static void heavy_work(void) {
volatile long sum = 0;
for (long i = 0; i < 50000000L; i++) {
sum += i; // Uses volatile to prevent the compiler from optimizing this away.
}
}
int main(void) {
// Get the current Unix timestamp with time().
time_t now = time(NULL);
printf("Unix timestamp: %ld\n", (long)now); // e.g., 1700000000
// Use difftime() to find the elapsed wall-clock time between two timestamps.
time_t start_cal = time(NULL);
heavy_work();
time_t end_cal = time(NULL);
double elapsed_sec = difftime(end_cal, start_cal);
printf("Wall clock elapsed: %.0f second(s)\n", elapsed_sec); // Elapsed time in whole seconds
// Use clock() to measure CPU time (excludes sleep and I/O wait time).
clock_t cpu_start = clock();
heavy_work();
clock_t cpu_end = clock();
double cpu_sec = (double)(cpu_end - cpu_start) / CLOCKS_PER_SEC;
printf("CPU time: %.4f second(s)\n", cpu_sec); // Output with millisecond precision.
return 0;
}
Notes
The time_t value returned by time() is the number of seconds since the epoch (00:00:00 UTC, January 1, 1970). When computing the difference between two time_t values, use difftime() instead of subtracting directly, as the underlying representation of time_t may vary by platform.
clock() returns CPU time used by the process, not wall-clock (real) elapsed time. Time spent sleeping or waiting for I/O is not included, making it well suited for measuring the performance of computation-heavy loops.
On 32-bit systems, time_t will overflow on January 19, 2038 (the Year 2038 problem). This is not an issue on 64-bit systems, but be careful when writing code for 32-bit environments such as embedded systems.
To convert a time_t value into a human-readable format, use localtime() / gmtime() or strftime().
If you find any errors or copyright issues, please contact us.