time (the time module)
The time module lets you measure how long something takes and pause your program with functions like time.time(), time.perf_counter() and time.sleep().
Used in 49 lessons of the course, from 00.03.
time is a standard library module. You bring it in with import time and call its functions with the prefix: time.sleep(1).
Three functions cover most needs:
time.sleep(seconds)pauses the program.time.time()returns the seconds since 1970 (the "epoch") as a float. It works as a timestamp.time.perf_counter()returns a high-precision counter. Only the difference between two calls means anything, so it is the best choice for measuring durations.
To measure, store the value at the start and subtract it at the end. Prefer perf_counter or monotonic over time.time(), because the system clock can be adjusted while you measure.
In the course you will see import time from lesson 00.03, mostly to measure how long a model call takes or to wait between retries.
Reach for it when: you need to pause the program, measure how long a block of code takes, or wait before a retry.
Examples
Common mistake
import time sleep(1)
Why: With `import time` the name `sleep` lives inside the module, not loose in your program.
How to write it: Write time.sleep(1), or use from time import sleep.
Common mistake
import time
time.sleep("2")Why: `sleep` expects a number of seconds, not a string.
How to write it: Pass a number: time.sleep(2), or convert with float("2").
