In Python, the time.monotonic()
function returns the value of a monotonic clock. It is used to measure elapsed time in a way that is not affected by system clock adjustments or changes due to time synchronization.
The monotonic clock is not affected by system time adjustments, such as changes made by the user or by the system itself for daylight saving time. This makes it useful for measuring time intervals or performance-related tasks where you need a reliable and consistent time reference.
The time.monotonic()
function returns a floating-point value representing the time in seconds since an unspecified starting point. It does not have a defined relationship to the time of day and should not be used for that purpose. The only guarantee is that the value will increase over time, so you can measure the difference between two calls to time.monotonic()
to calculate elapsed time.
Here’s a simple example that demonstrates the usage of time.monotonic()
to measure the execution time of a code block:
import time
start_time = time.monotonic()
# Code block to measure execution time
# ...
end_time = time.monotonic()
elapsed_time = end_time - start_time
print("Elapsed time:", elapsed_time, "seconds")
By subtracting the start time from the end time, you can determine the elapsed time in seconds. It’s important to note that the value returned by time.monotonic()
represents relative time and is not related to the system’s wall clock time.
what is the difference between time.monotonic and time.time?
time.monotonic()
:- It returns the value of a monotonic clock in seconds (floating-point).
- The unit of measurement for
time.monotonic()
is seconds. - It provides a reliable and consistent time reference for measuring elapsed time, but it does not have a defined relationship with the time of day.
time.time()
:- It returns the current time as a floating-point value representing the number of seconds since the Unix epoch (January 1, 1970, at 00:00:00 UTC).
- The unit of measurement for
time.time()
is also seconds. - Although the function returns the time in seconds, it can often include a fractional part representing milliseconds or smaller units, depending on the platform.
Another way is time.perf_counter()
Here’s an example that demonstrates how to calculate the elapsed time in milliseconds:
import time
start_time = time.perf_counter()
# Code block to measure execution time
# ...
end_time = time.perf_counter()
elapsed_time_ms = (end_time - start_time) * 1000 # Convert to milliseconds
print("Elapsed time:", elapsed_time_ms, "milliseconds")
In the above code, time.perf_counter()
is used to obtain high-resolution time measurements, typically in fractional seconds. By subtracting the start time from the end time, you get the elapsed time in seconds. Multiplying it by 1000 converts it to milliseconds.
Keep in mind that time.perf_counter()
measures the time elapsed between two points in your code, including any sleep time or system-level delays. If you want to measure the time for specific code blocks excluding delays, you may need to adjust the timing logic accordingly.
By using time.perf_counter()
, you can achieve more precise measurements of elapsed time in milliseconds.