Skip to content
Tutorials Center
My learning

Topics

C Reference ยท ctype.h, math.h and others

C time() and clock() functions

Reference for time.h basics: time() for calendar time, clock() for processor time, CLOCKS_PER_SEC, difftime() and how to measure elapsed time safely.

<time.h> offers two basic clocks. time() returns the calendar time: a number that counts seconds from a fixed starting point, the epoch. On Linux and macOS the epoch is 1970-01-01 00:00:00 UTC, but ISO C does not require that, and time_t may be an integer or a floating-point type. clock() returns the processor time your program has used so far, in units of 1 / CLOCKS_PER_SEC seconds.

Use time() to answer "what moment is it?" and clock() to measure how much CPU a piece of code consumed. Neither is the right tool for precise wall-clock timing; see timespec_get and clock_gettime.

C
#include <time.h>

time_t now = time(NULL);
clock_t start = clock();

Try it

C time() and difftime()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Measuring CPU time with clock()

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
time_t  time(time_t *timer);
clock_t clock(void);
double  difftime(time_t end, time_t beginning);
Name Description
timer If not NULL, the result is also stored here. Passing NULL is the usual style.
end, beginning Two calendar times for difftime().

Returns: time() returns the current calendar time, or (time_t)(-1) if it is not available. clock() returns the processor time used since the program started, or (clock_t)(-1) if unavailable. difftime() returns end - beginning in seconds as a double.

Errors / undefined behavior: none are defined beyond the failure values. clock_t can wrap around on systems where it is a 32-bit integer.

Usage notes

  • time_t is opaque in ISO C. Do not do arithmetic on it by hand if you want portable code; use difftime(). In practice, on 64-bit Linux, macOS and modern Windows it is a 64-bit count of seconds, so t + 86400 really is one day later (ignoring leap seconds and time zones).
  • Year 2038: systems that still use a 32-bit signed time_t overflow on 2038-01-19. Check sizeof(time_t) on embedded targets.
  • time() has one-second resolution and can jump when the system clock is adjusted. Do not use it to time a loop; use clock_gettime(CLOCK_MONOTONIC, ...).
  • clock() measures CPU time, not wall-clock time. A program that sleeps or waits for input barely advances it. With several threads, glibc adds up the CPU time of all of them, so clock() can run faster than the wall clock.
  • CLOCKS_PER_SEC is 1,000,000 on POSIX systems, but always divide by the macro instead of hard-coding it. Cast to double before dividing, or the integer division discards the fraction.
  • Seeding random numbers with time(NULL) is common (srand((unsigned)time(NULL))) but gives the same seed to programs started in the same second. See rand and srand.
  • Make time-dependent code testable: pass a time_t into your function instead of calling time() inside it, so tests can use fixed values like the example above.
  • To turn a time_t into a calendar date, use gmtime() or localtime() and then strftime().
  • Standard version: time, clock and difftime are C89. C11 adds timespec_get.