# Timing Your Code with the time Module
# sleep, time(), perf_counter — pauses and measurements
# redhorndev.com

import time

# ─────────────────────────────────────────────
# time.sleep() — pause execution
# ─────────────────────────────────────────────

print("Starting...")
time.sleep(2)               # pause 2 seconds
print("Done.")

time.sleep(0.5)             # half a second
time.sleep(0.1)             # 100 milliseconds

# ─────────────────────────────────────────────
# time.time() — measure elapsed time
# ─────────────────────────────────────────────

start = time.time()

total = 0
for i in range(1_000_000):
    total += i

end = time.time()
print(f"Elapsed: {end - start:.4f} seconds")
# Elapsed: 0.0812 seconds

# ─────────────────────────────────────────────
# perf_counter() — more precise
# ─────────────────────────────────────────────

start = time.perf_counter()
total = sum(range(1_000_000))
end   = time.perf_counter()

print(f"Elapsed: {end - start:.6f} seconds")
# Elapsed: 0.023451 seconds

# ─────────────────────────────────────────────
# Practical — countdown timer
# ─────────────────────────────────────────────

for i in range(5, 0, -1):
    print(f"{i}...")
    time.sleep(1)

print("Go.")

# 5... (1 second pause)
# 4... (1 second pause)
# 3...
# 2...
# 1...
# Go.

# ─────────────────────────────────────────────
# Practical — measure a function
# ─────────────────────────────────────────────

def slow_function():
    time.sleep(1.5)
    return "done"

start = time.perf_counter()
result = slow_function()
end   = time.perf_counter()

print(f"{result} — took {end - start:.2f}s")
# done — took 1.50s

# ─────────────────────────────────────────────
# Quick reference
# ─────────────────────────────────────────────

# import time
#
# time.sleep(n)           — pause n seconds (decimals ok)
# time.time()             — seconds since epoch (float)
# time.perf_counter()     — high-resolution timer
#
# elapsed = end - start   — always the same pattern
# time  → execution control and measurement
# datetime → calendar dates and timestamps
