04.12.2022 - Reading Notes/Wall Clock and Monotonic Clock
In a computer system, there are 2 types of clocks:
- Wall Clock (or Real-Time Clock): is the clock that is synchronized with NTP (Network Time Protocol), which is subjected to jump (moving forward or backward) depending on the time server.
- Monotonic Clock: is the clock that is guaranteed to only move forward, regardless of the time server. The frequency rate of this clock might vary, it can be adjusted if the system detects the local quartz is moving faster or slower than the NTP. But it’s guaranteed to always move forward.
Go’s documentation suggested that we should use wall clock only to tell the time, and use monotonic clock to measure the time.
const start = Date.now(); doSomeWork(); const end = Date.now(); const elapsed = end - start;
Because Date.now() is using wall clock, if the system time jumps between the measurement, the elapsed time will be incorrect.
const start = performance.now(); doSomeWork(); const end = performance.now(); const elapsed = end - start;
Monotonic clock time can be obtained in some other languages like:
- std::time::Instant in Rust
- time.monotonic() in Python
- time.Sub(startTime) in Golang
- hrtime in NodeJS
It is important to note that, monotonic clock is only correct locally. In a distributed system, it is a different topic.
- 04.15.2022 - Math/Prime Factorization Uniqueness
- 04.14.2022 - TypeScript/Understanding “keyof typeof”
- 04.13.2022 - Reading Notes/Detect Infinite Recursive Call with Control Flow Graph
- 04.11.2022 - TypeScript/Use String As Enum Key
- 04.10.2022 - Algorithms/Tracking letter frequency with Hashmap and Array