The Python time module is a standard library used to access and convert time, providing functions to get the current time, format strings, measure execution duration, and pause execution. It operates primarily using Unix timestamps, which represent the number of seconds (or nanoseconds) elapsed since the epoch on January 1, 1970, 00:00:00 UTC.
Key functions within the module include:
time.time(): Returns the current time as a floating-point number of seconds since the epoch.time.perf_counter(): Measures elapsed time with high precision, ideal for benchmarking code performance.time.sleep(seconds): Suspends the execution of the current thread for a specified number of seconds.time.ctime(): Converts a time in seconds since the epoch into a human-readable string representing local time.time.strftime()andtime.strptime(): Format time tuples into strings or parse strings back into time tuples using specific directives.time.monotonic(): Returns the value of a monotonic clock that cannot go backward, ensuring reliable time intervals.
To use these functions, you must first import the module with import time. For example, getting the current time in seconds and converting it to a readable string looks like this:
import time
# Get current time in seconds since epoch
current_seconds = time.time()
# Convert to a readable local time string
readable_time = time.ctime(current_seconds)
print(f"Seconds: {current_seconds}")
print(f"Readable: {readable_time}")While time handles low-level timestamps and system time, the datetime module is often preferred for working with dates, formatting, and time zones in a more object-oriented manner. The time module is essential for tasks like benchmarking, scheduling delays, and generating logs with precise timestamps.
Videos
Use time.time() to measure the elapsed wall-clock time between two points:
import time
start = time.time()
print("hello")
end = time.time()
print(end - start)
This gives the execution time in seconds.
Another option since Python 3.3 might be to use perf_counter or process_time, depending on your requirements. Before 3.3 it was recommended to use time.clock (thanks Amber). However, it is currently deprecated:
On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name.
On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function
QueryPerformanceCounter(). The resolution is typically better than one microsecond.Deprecated since version 3.3: The behaviour of this function depends on the platform: use
perf_counter()orprocess_time()instead, depending on your requirements, to have a well defined behaviour.
Use timeit.default_timer instead of timeit.timeit. The former provides the best clock available on your platform and version of Python automatically:
from timeit import default_timer as timer
start = timer()
# ...
end = timer()
print(end - start) # Time in seconds, e.g. 5.38091952400282
timeit.default_timer is assigned to time.time() or time.clock() depending on OS. On Python 3.3+ default_timer is time.perf_counter() on all platforms. See Python - time.clock() vs. time.time() - accuracy?
See also:
- Optimizing code
- How to optimize for speed