How to access the monotonic "steady" clock?

POSIX systems offer a “system clock”, represented as time since the UNIX epoch (1-1-1970), nowadays usually represented with a nanoseconds resolution. There’s also supposed to be a “monotonic clock”, also known as “steady_clock” in the C++ chrono library, which is usually counting from the time the computer last booted, and it’s guaranteed to be monotonic. the now() and time() functions seem to return the system time always with the full date. Is there any Julia standard function to load the monotonic clock, or steady clock?

2 Likes

time_ns() uses uv_hrtime(), which is guaranteed to be monotonic (mod 2⁶⁴, so that time differences will increase monotonically as long as the system is running).

See also the discussion in tic() should use monotonic clock · Issue #2464 · JuliaLang/julia · GitHub … I submitted a PR to clarify the documentation for time_ns: clarify that time_ns is monotonic by stevengj · Pull Request #57129 · JuliaLang/julia · GitHub

(time_ns almost surely uses the same underlying system call as std::chrono::steady_clock. e.g. it uses the POSIX clock_gettime(CLOCK_MONOTONIC, &t) function on Unix. This is not “the calendar time since the UNIX epoch”, by the way … it’s usually related to time since the last reboot as you noted, though its behavior on system sleep is OS-dependent.)

5 Likes

Perfect, thank you (again)! :slight_smile: