Definition
A monotonic clock is a clock whose readings never decrease: on one machine, a reading taken later is greater than or equal to any reading taken earlier. It exists to measure elapsed time, by subtracting one reading from another. Its zero point is arbitrary, often the moment the machine started, so a single reading is not a date, and readings from different machines cannot be compared directly. Unlike a wall clock §9.36, which shows the date and time, it does not jump when the system’s date and time are set.
The idea
A program measures an operation by reading the system’s date-and-time clock before and after it: 12:00:00.250 and then 12:00:00.375, a duration of 125 milliseconds (ms). During one such measurement, time synchronization software finds that the system clock is 1 s fast and sets it back by 1 s. The second reading becomes 11:59:59.375, and the computed duration is ms: a negative duration for an operation that took 125 ms. The operation was fine; the instrument was wrong.
A stopwatch is the right picture. A race is timed with a stopwatch, not by reading a station clock that staff may reset, and the stopwatch’s display means nothing on its own: only the difference between start and stop matters. The picture stops being accurate in one respect. Depending on the system, a monotonic clock may stop counting while the machine is suspended, like a stopwatch that pauses whenever its owner sleeps.
How it works
A monotonic clock is usually built on a hardware counter that counts the ticks of an oscillator, an electronic circuit that produces a steady frequency, from some starting point such as system start. The operating system converts the count into seconds or nanoseconds. A program measures an interval by reading the same clock at the start and at the end and subtracting. Six properties of the clock are separate questions, and the word monotonic answers only the first:
Never decreasing. Two readings may be equal: readings taken within one tick of the counter return the same value. Monotonic does not mean strictly increasing.
Resolution: the smallest nonzero difference two readings can show.
Rate adjustment: whether time synchronization software, such as a client of the Network Time Protocol (NTP), may make the clock run slightly faster or slower to correct its rate. This is called slewing, and it never makes the clock go backward.
Suspend behavior: whether the clock keeps counting while the machine is suspended, that is, put into a low-power sleep state from which it later resumes.
Reference point: where zero is. It is unspecified, differs between machines and usually changes at every restart.
Width: on small devices the counter has a fixed number of bits and returns to zero after its largest value, which is called counter wraparound §6.21. A raw wrapping counter is not monotonic until software extends it to more bits.
Linux offers several clocks through the function clock_gettime. CLOCK_MONOTONIC never jumps and is slewed by time synchronization; it does not count time while the system is suspended. CLOCK_BOOTTIME is the same except that it does count suspended time. CLOCK_MONOTONIC_RAW is not slewed: it follows the hardware oscillator. CLOCK_REALTIME is the wall clock and can jump in both directions.
The Python §12.23 time module offers these functions:
time.monotonic()returns a monotonic clock reading in seconds as a floating-point number §4.49, andtime.monotonic_ns()returns the same clock as an integer number of nanoseconds. The documentation states that the reference point is undefined, so that only the difference between two results is valid. On Linux the function callsclock_gettime(CLOCK_MONOTONIC).time.perf_counter()andtime.perf_counter_ns()read the clock with the highest available resolution for measuring short durations. Since Python 3.13, the standard implementation of Python, CPython, uses the same clock for them as fortime.monotonic().time.time()reads the wall clock: seconds since 1 January 1970, 00:00:00 UTC (Coordinated Universal Time). It can jump forward or backward whenever the system time is set, so it is unsuitable for measuring durations.time.get_clock_info(name)describes the clock behind one of these functions. Its result has four fields:implementation, the underlying C function;monotonic, true if the clock cannot go backward;adjustable, true if the clock can be set to jump forward or backward (gradual rate adjustments do not count); andresolution, in seconds. Recording this result beside a measurement tells a later reader which clock produced the numbers and what that clock promised.
Worked example
A monotonic clock reads 12000.250 s at the start of an operation and 12000.375 s at its end. The interval is s, which is 125 ms. The large part, 12000 s, is the time since the clock’s arbitrary zero point, and it cancels in the subtraction. A second machine whose monotonic clock reads 8000.375 s at the same instant gives a reading that cannot be compared with 12000.250 at all, because the two clocks have different zero points.
A clock with a resolution of 1 ms returns the same reading for two calls made within one 1 ms tick. Their difference is 0 ms although a little time passed. The clock kept its promise, because never decreasing does not promise strictly increasing. In general, if both readings are cut down to whole ticks of length and their difference is ticks, the true interval lies strictly between and ; a difference of 0 ticks means an interval shorter than one tick. This effect is counter quantization §11.35.
A host-timed measurement in Python reads the clock twice and subtracts:
t0 = time.perf_counter_ns()
do_work()
elapsed_ns = time.perf_counter_ns() - t0
The two instants that bound the measurement are the two calls. On Linux with CPython 3.13 or later, the call
time.get_clock_info('perf_counter')
reports the implementation clock_gettime(CLOCK_MONOTONIC), monotonic=True and adjustable=False, and its resolution field gives the clock’s resolution, typically 1e-09 s. A reader learns from this record that the clock cannot go backward and cannot be set to jump; the implementation name CLOCK_MONOTONIC further tells the reader that, on Linux, suspended time is not counted.
The integer functions exist because of floating-point precision. A 64-bit floating-point number has 53 significant bits. time.time() now returns about s, a value between and , where adjacent representable numbers are s apart, about 238 nanoseconds. A floating-point value of that size cannot hold a nanosecond reading. For a monotonic reading of 12000 s, between and , the spacing is s, about 1.8 picoseconds, so the loss there is negligible.
Stated precisely
Let be the reading of a monotonic clock at true time , on one machine and within one run of that machine between restarts. The contract is: if , then . The measured interval is therefore never negative.
is exact in the clock’s own units. Turning it into the true elapsed time needs three more facts about the clock. With resolution , differs from the clock’s exact count by less than . With a rate error of at most , a fraction such as 50 parts per million, the clock’s exact count over a true interval differs from by at most . A clock that pauses during suspend gives a shorter than by the suspended time.
Nothing in the contract relates readings of different clocks, of different machines, or of one machine before and after a restart.
Limits and common mistakes
Monotonic is not accurate. A monotonic clock runs fast or slow by its oscillator’s error, called clock drift §6.18, which for a quartz crystal is typically tens of parts per million. Monotonic only promises that the readings never go backward.
Monotonic is not shared. Readings from two machines, or from one machine across a restart, have unrelated zero points. Comparing them needs clock synchronization, which estimates how two clocks relate.
Suspend behavior must be chosen. To measure how long an operation ran while the machine was awake, exclude suspended time; to measure how long a user waited, include it. On Linux the first is CLOCK_MONOTONIC and the second CLOCK_BOOTTIME.
Nanosecond units are not nanosecond accuracy. An integer nanosecond reading avoids floating-point rounding; it says nothing about the clock’s resolution or its rate error.
A fixed-width hardware counter wraps around. A 32-bit counter that advances 64,000,000 times per second returns to zero every s. Subtracting two readings with unsigned arithmetic gives the right interval only while the interval is shorter than that wrap period.
Questions answered
Does this clock include suspend time and have the same epoch on other hosts?
Whether a monotonic clock includes suspend time depends on the specific clock, and its epoch is not shared with other hosts: monotonic only promises that readings on one machine never decrease. Suspend time is the time the machine spends in a low-power sleep state between suspending and resuming. On Linux, CLOCK_MONOTONIC does not count it and CLOCK_BOOTTIME does. Python’s time.monotonic() uses CLOCK_MONOTONIC on Linux, so it excludes suspend time there. Check the documentation of the clock actually used, and record time.get_clock_info() or its equivalent with the measurement.
The epoch, the point the clock counts from, is unspecified: the Python documentation states that the reference point of time.monotonic() is undefined, so only the difference between two readings of the same clock is valid. In practice the epoch is often the moment the machine started, which differs between machines and between restarts. Readings from two hosts can be compared only after both are mapped onto one time base through clock synchronization, with the uncertainty of that mapping carried into the result.
Related entries: Wall clock and clock synchronization (§9.36), Counter quantization (§11.35), Deadline versus timeout (§9.34), Counter wraparound (§6.21), Clock source and drift (§6.18), Clock domain (§13.29). Further reading: [33].