The C standard, and Unix since at least 7th Edition UNIX™, has provided single-second accuracy with the time()
function:
time_t now = time(0);
printf("%ld\n", (long)now);
7th Edition UNIX™ (or Version 7 UNIX™) provided millisecond resolution with ftime()
— and it was included in POSIX up to and including the 2004 version (ftime()
), but it is not part of POSIX 2008 or later, though it will still be supported on some machine types for reasons of backwards compatibility.
#include <sys/timeb.h>
struct timeb tb;
if (ftime(&tb) == 0)
printf("%ld.%.3d\n", (long)tb.time, tb.millitm);
POSIX also provided (and still provides) microsecond resolution timing via struct timeval
and gettimeofday()
. It is marked obsolescent in POSIX 2008. It may be the most portable timer.
#include <sys/time.h>
struct timeval tv;
if (gettimeofday(&tv, NULL) == 0)
printf("%ld.%.6d\n", (long)tv.tv_sec, tv.tv_usec);
Note that there are caveats about using gettimeofday()
— its result can be affected if someone adjusts the system between successive calls. Similar comments apply to clock_gettime()
and CLOCK_REALTIME
.
POSIX is moving towards using nanosecond resolution timing, which is provided via struct timespec
and clock_gettime()
. With clock_gettime()
, you must choose which clock you wish to measure. For many purposes, CLOCK_REALTIME
is the correct choice (but CLOCK_MONOTONIC
may be better for some purposes, if it is available).
#include <time.h>
struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
printf("%ld.%.9d\n", (long)ts.tv_sec, ts.tv_nsec);
I'm not sure exactly where clock()
came from. It wasn't in 7th Edition Unix, but it was in SVR4 with 100 for CLOCKS_PER_SEC. The C standard provides the clock()
function (the POSIX specification for (clock()
requires 1,000,000 for CLOCKS_PER_SEC; C does not). Note that this does not measure elapsed 'wall time'; it measures an approximation to the amount of CPU time that a process has used.
The clock()
function shall return the implementation's best approximation to the processor time used by the process since the beginning of an implementation-defined era related only to the process invocation.
To determine the time in seconds, the value returned by clock()
should be divided by the value of the macro CLOCKS_PER_SEC
. [XSI] ⌦ CLOCKS_PER_SEC is defined to be one million in <time.h>
. ⌫
clock_t clk = clock();
printf("%.6f\n", (double)clk / CLOCKS_PER_SEC);
Since the clock()
function measures the CPU time used, not the wall clock time that elapses, it is wholly inappropriate to measure elapsed time when sleep()
is used because a sleeping process uses no CPU time.
The C11 standard provides some thread-related functions which use struct timespec
, which is defined to match the POSIX definition of the type. It also provides the function timespec_get()
:
7.27.2.5 The timespec_get
function
#include <time.h>
int timespec_get(struct timespec *ts, int base);
¶2 The timespec_get
function sets the interval pointed to by ts
to hold the current calendar time based on the specified time base.
¶3 If base is TIME_UTC
, the tv_sec
member is set to the number of seconds since an implementation defined epoch, truncated to a whole value and the tv_nsec
member is set to the integral number of nanoseconds, rounded to the resolution of the system clock.321)
¶4 If the timespec_get
function is successful it returns the nonzero value base
; otherwise, it returns zero.
321) Although a struct timespec
object describes times with nanosecond resolution, the available resolution is system dependent and may even be greater than 1 second.
This function may not be widely available yet. It is not available on macOS 10.14.5 Mojave, for example, though it seems to be available in OpenBSD. It may be available in glibc
(GNU C Library) but it isn't listed in the Linux man pages (neither in section 2 system calls or section 3 functions at https://linux.die.net, nor in section 2 system calls or section 3 functions at https://man7.org/). Equally clearly, it's trivial to implement a decent approximation to it if clock_gettime()
is available:
#if !defined(HAVE_TIMESPEC_GET) && defined(HAVE_CLOCK_GETTIME)
enum { TIME_UTC = 1 };
static inline int timespec_get(struct timespec *ts, int base)
{
assert(base != 0);
if (clock_gettime(CLOCK_REALTIME, ts) != 0)
return 0;
return base;
}
#endif