📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
The monotonic
function in Python's time
module returns the value (in fractional seconds) of a monotonic clock, i.e., a clock that cannot go backwards. This function is useful for measuring elapsed time without being affected by system clock updates.
Table of Contents
- Introduction
monotonic
Function Syntax- Examples
- Basic Usage
- Measuring Elapsed Time
- Real-World Use Case
- Conclusion
Introduction
The monotonic
function in Python's time
module provides a way to measure elapsed time that is guaranteed not to move backward, making it ideal for timing operations. This function is unaffected by system clock updates, ensuring consistent measurements.
monotonic Function Syntax
Here is how you use the monotonic
function:
import time
current_time = time.monotonic()
Parameters:
- The
monotonic
function does not take any parameters.
Returns:
- A floating-point number representing the current value of the monotonic clock in seconds.
Examples
Basic Usage
Here is an example of how to use monotonic
.
Example
import time
# Getting the current monotonic time
start_time = time.monotonic()
print("Monotonic start time:", start_time)
Output:
Monotonic start time: 39520.609
Measuring Elapsed Time
This example shows how to measure the elapsed time of a code block using monotonic
.
Example
import time
# Starting the monotonic timer
start_time = time.monotonic()
# Code block whose execution time is to be measured
for i in range(1000000):
pass
# Stopping the monotonic timer
end_time = time.monotonic()
# Calculating the elapsed time
elapsed_time = end_time - start_time
print("Elapsed time:", elapsed_time, "seconds")
Output:
Elapsed time: 0.030999999995401595 seconds
Real-World Use Case
Timing Operations Without Clock Skew
In real-world applications, the monotonic
function can be used to measure the duration of operations without being affected by changes to the system clock, such as daylight saving time adjustments or manual changes.
Example
import time
def simulate_operation():
time.sleep(2) # Simulate a long-running operation
# Timing the operation
start_time = time.monotonic()
simulate_operation()
end_time = time.monotonic()
# Calculating the elapsed time
elapsed_time = end_time - start_time
print(f"Operation took {elapsed_time:.6f} seconds")
Output:
Operation took 2.000000 seconds
Conclusion
The monotonic
function provides a reliable way to measure elapsed time without being affected by system clock changes. This makes it ideal for timing operations and ensuring consistent performance measurements.
Comments
Post a Comment
Leave Comment