time() / difftime() / clock()
| Since: | C89(1989) |
|---|
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
// If t is not NULL, the result is also stored in *t. time_t time(time_t *t); // Return value: the difference time1 - time0, as a double (in seconds). double difftime(time_t time1, time_t time0); // 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
sample_time_difftime_clock.c
#include <stdio.h>
#include <time.h>
/* Uses volatile to prevent the compiler from optimizing away the loop. */
static void heavy_work(void) {
volatile long sum = 0;
for (long i = 0; i < 50000000L; i++) {
sum += i;
}
}
int main(void) {
time_t now = time(NULL);
printf("Unix timestamp: %ld\n", (long)now);
/* Use difftime() to measure wall-clock elapsed time. */
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);
/* Use clock() to measure CPU 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);
return 0;
}
Run the following command:
gcc sample_time_difftime_clock.c -o sample_time_difftime_clock ./sample_time_difftime_clock Unix timestamp: 1743379805 Wall clock elapsed: 0 second(s) CPU time: 0.0823 second(s)
Recording Timestamps in a Log
A common pattern is to record the start and end times of a processing block using time(), then compute elapsed seconds with difftime().
time_log_session.c
#include <stdio.h>
#include <time.h>
/* Dummy function to simulate work. */
static void process_data(void) {
volatile long x = 0;
for (long i = 0; i < 10000000L; i++) { x += i; }
}
int main(void) {
time_t session_start = time(NULL);
printf("Start: timestamp = %ld\n", (long)session_start);
process_data();
process_data();
time_t session_end = time(NULL);
double total = difftime(session_end, session_start);
printf("End: timestamp = %ld\n", (long)session_end);
printf("Total elapsed: %.0f second(s)\n", total);
/* Also report CPU time with clock(). */
clock_t t0 = clock();
process_data();
clock_t t1 = clock();
printf("CPU time (one call): %.4f second(s)\n", (double)(t1 - t0) / CLOCKS_PER_SEC);
return 0;
}
Compile and run with the following command:
gcc time_log_session.c -o time_log_session ./time_log_session Start: timestamp = 1743379805 End: timestamp = 1743379805 Total elapsed: 0 second(s) CPU time (one call): 0.0165 second(s)
Common Mistakes
Common Mistake: Subtracting time_t Values Directly
The underlying type of time_t can vary by platform. Using difftime() instead of direct subtraction improves portability.
time_subtract_ng.c
#include <stdio.h>
#include <time.h>
int main(void) {
time_t t1 = time(NULL);
volatile long x = 0;
for (long i = 0; i < 30000000L; i++) { x += i; }
time_t t2 = time(NULL);
/* NG: casting time_t difference to double (type-dependent) */
/* double ng = t2 - t1; */
/* OK: use difftime() for portable subtraction */
double ok = difftime(t2, t1);
printf("Elapsed: %.0f second(s)\n", ok);
return 0;
}
Run the following command:
gcc time_subtract_ng.c -o time_subtract_ng ./time_subtract_ng Elapsed: 0 second(s)
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.