TIL: System Clock and Monotonic Clock
Did you know that every computer/device has two clocks in it? A system clock and a monotonic clock.
The system clock shows the wall-clock time and may jump forward or backward because of the user changing the time manually, or based on the network time.
But in the monotonic clock, the time always moves forward at a constant rate and is not affected by the system clock. The start time of the monotonic clock can be any arbitrary time, including 0.
When you measure your code performance, use the monotonic clock in your language. Or when you do time-out functions (like banking sites logging out after 5 minutes), use the monotonic clock.
Here’s how to access the monotonic clock in some languages:
- Java:
System.nanoTime()
- Python:
time.monotonic()
- Javascript:
performance.now()
- PHP:
hrtime()
Check the manual of your language to find the function that returns the monotonic time.