126 days ago · Tech · hide

Today I learned that time.time() and time.perf_counter() are not interchangeable, even though both return timestamps as floating-point seconds. time.time() Returns the current wall-clock time as seconds since the Unix Epoch (1970-01-01 00:00:00 UTC). import time now = time.time() print(now) # e.g. 1784611234.123456 Use time.time() when You need the actual current date/time. Creating timestamps. Calculating expiration times (TTLs). Logging events. Caveat The system clock can be adjusted (e.g. NTP sync, manual clock change). This means the value can jump forward or backward, making it unreliable for measuring elapsed durations. time.perf_counter() Returns a high-resolution, monotonic performance counter. The reference point is undefined—only differences between two calls are meaningful. import time start = time.perf_counter() # code to measure sum(range(1000000)) elapsed = time.perf_counter() - start print(elapsed) Use time.perf_counter() when Benchmarking code. Measuring latency.…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town