Python time.time vs monotonic vs perf_counter

Python's time module provides multiple functions for measuring time, each tailored to specific operational needs. While time.time() measures real-world wall-clock time, time.monotonic() provides a steady, non-decreasing clock ideal for long-running intervals, and time.perf_counter() delivers the highest-resolution clock available for accurate performance benchmarking. Choosing the correct function prevents bugs caused by system clock adjustments, daylight saving changes, or inadequate measurement resolution.

time.time(): The Wall-Clock Time

time.time() returns the current Unix timestamp—the number of seconds that have elapsed since January 1, 1970, 00:00:00 (UTC).

time.monotonic(): The Unchanging Timeline

time.monotonic() returns a floating-point value representing seconds from an arbitrary reference point, such as system boot time.

time.perf_counter(): High-Resolution Benchmarking

time.perf_counter() provides the highest available resolution clock on the host platform, designed specifically for measuring short execution times.

Summary Comparison

Function Primary Purpose Can Move Backwards? Best Scenario
time.time() Wall-clock time Yes (NTP/manual changes) Timestamps, logging, display
time.monotonic() Elapsed time No Timeouts, rate limits, loops
time.perf_counter() Precision timing No Benchmarking, profiling code