Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

C++ chrono clocks: steady_clock and system_clock

Reference for std::chrono clocks: steady_clock for measuring elapsed time, system_clock for calendar time, time_point arithmetic and benchmarking pitfalls.

A clock in <chrono> is a type with a now() function that returns a time_point: a moment measured as a duration since that clock's epoch. The standard defines three general-purpose clocks. std::chrono::system_clock is the wall clock of the operating system, mapped to civil time and convertible to calendar dates. std::chrono::steady_clock is monotonic: it never goes backwards, which makes it right for measuring how long something took. std::chrono::high_resolution_clock is the finest tick period the implementation offers, but it is usually just an alias of one of the other two.

C++20 adds utc_clock, tai_clock, gps_clock and file_clock for more specialized needs.

C++
auto start = std::chrono::steady_clock::now();
work();
auto elapsed = std::chrono::steady_clock::now() - start;   // a duration

Try it

C++ Measuring elapsed time

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 std::cin or std::getline), it gets the next line from this box.

C++ system_clock, time_t and conversion to dates

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 std::cin or std::getline), it gets the next line from this box.

Syntax

C++
struct some_clock {
    using rep = /* arithmetic */;
    using period = /* std::ratio */;
    using duration = std::chrono::duration<rep, period>;
    using time_point = std::chrono::time_point<some_clock>;
    static constexpr bool is_steady = /* ... */;
    static time_point now() noexcept;
};

template<class Clock, class Duration = typename Clock::duration> class time_point;
Member Description
now() Current time point of the clock.
is_steady true if time points never decrease and the interval between ticks is constant.
duration, rep, period Tick representation and length.
system_clock::to_time_t, from_time_t Convert to and from C's time_t.
time_since_epoch() The duration since the clock's epoch (a time_point member).
time_point_cast<D>(tp), floor, ceil, round Change the time point's duration type.

Returns: now() returns a time_point; subtracting two time points of one clock returns a duration.

Errors / undefined behavior: you cannot subtract or compare time points of different clocks directly (compile error). Converting a time_point that does not fit time_t is unspecified. now() does not throw in practice.

Usage notes

  • Use steady_clock to measure. system_clock can jump backwards or forwards when the user or NTP adjusts the system time; a duration computed from it can be wrong or even negative.
  • Use system_clock to date. It is the only general-purpose clock that corresponds to real-world dates, and since C++20 its epoch is specified as the Unix epoch.
  • The epoch of steady_clock is arbitrary (often system start-up). Its time points are meaningless on their own: only differences matter.
  • high_resolution_clock is an alias for system_clock on some implementations and steady_clock on others (GCC uses system_clock). Prefer steady_clock for timing; do not rely on it being finer.
  • Benchmarking caveats: the optimizer can delete code whose result is unused; run enough iterations, use the result (print it or store it in a volatile), and expect noise from caches, frequency scaling and other processes.
  • Resolution: check steady_clock::period, but do not assume more precision than your operating system provides; a call to now() itself costs tens of nanoseconds.
  • Time points are cheap value types. Store them in structs and compare them with <; add a duration to get a deadline for wait_until, sleep_until, or a timed lock.
  • Do not print now() in tests; wall-clock values change on every run.
  • C++20 formatting: std::cout << sys_seconds and std::format("{:%F %T}", tp) print UTC timestamps.