The time Module, Delays & Performance Benchmarking
While datetime is built for calendar dates, human months, and clock time, Python's built-in time module is designed for system timers, epoch timestamps, and execution delays.
1. Introducing Delays with time.sleep()
time.sleep(seconds) pauses the execution of your Python script for the specified number of seconds (can be a float for milliseconds):
import time
print("Sending verification OTP to user...")
# Pause execution for 2 seconds
time.sleep(2)
print("OTP sent successfully!")
# Fractional seconds (e.g. 500 milliseconds):
time.sleep(0.5)
Real-World Use Case: Polite Web Scraping & Polling
When calling third-party APIs or polling background servers for task status, adding a 1–2 second sleep prevents your IP address from getting rate-limited or blocked.
2. Unix Epoch Timestamps with time.time()
Computers store time as the number of seconds that have passed since January 1, 1970 UTC (known as the Unix Epoch):
import time
epoch_seconds = time.time()
print("Epoch timestamp:", epoch_seconds) # Output: 1786184710.123456
3. High-Precision Benchmarking with time.perf_counter()
When measuring how fast an algorithm, database query, or sorting function executes, never use time.time() (which can be affected by system clock resets).
Always use time.perf_counter(), which provides ultra-high resolution nanosecond hardware timers:
import time
def benchmark_calculation():
start = time.perf_counter()
# Run expensive operation: sum of squares up to 1,000,000
total = sum(x ** 2 for x in range(1000000))
end = time.perf_counter()
execution_time = end - start
print(f"Calculation completed in: {execution_time:.6f} seconds.")
benchmark_calculation()
Quick Summary
| Feature | time module | datetime module |
|---|---|---|
| Primary Focus | Hardware clock, pauses, seconds | Human calendar dates & formatting |
| Delay execution | time.sleep(seconds) | Not supported |
| Benchmarking | time.perf_counter() | Not designed for high-resolution nanoseconds |
| Parsing strings | time.strptime() | datetime.strptime() (Recommended) |
What's Next?
Let's explore mathematical constants, rounding, factorials, and trigonometric calculations in The math Module!