Open In App

Python | time.monotonic() method

Improve
Improve
Like Article
Like
Save
Share
Report

Python time.monotonic() method is used to get the value of a monotonic clock. A monotonic clock is a clock that can not go backward.  As the reference point of the returned value of the monotonic clock is undefined, only the difference between the results of consecutive calls is valid.

Python time.monotonic() method Syntax:

Syntax: time.monotonic()

Parameter: No parameter is required.

Return type: This method returns a float value which represents the value of a monotonic clock in fractional seconds. 

Python time monotonic() example

Example 1: Use of time.monotonic() method to get the value of a monotonic clock 

Python3




# Python program to explain time.monotonic() method
 
# importing time module
import time
 
# Get the value of
# a monotonic clock using
# time.monotonic() method
value = time.monotonic()
 
# print the value of
# the monotonic clock
print("Value of the monotonic clock (in fractional seconds):", value)


Output:

Value of the monotonic clock (in fractional seconds): 216444.515

Example 2: Use of time.monotonic() method to measure elapsed time in long-running process

Python3




# Python program to explain time.monotonic() method
 
# importing time module
import time
 
# Get the value of
# a monotonic clock at the
# beginning of the process
# using time.monotonic() method
start = time.monotonic()
 
# print the value of
# the monotonic clock
print("At the beginning of the process")
print("Value of the monotonic clock (in fractional seconds):", start)
 
i = 0
arr = [0] * 10
while i < 10:
    # Take input from the user
    arr[i] = int(input())
    i = i + 1
 
# Print the user input
print(arr)
 
# Get the value of
# monotonic clock
# using time.monotonic() method
end = time.monotonic()
 
# print the value of
# the monotonic clock
print("\nAt the end of the process")
print("Value of the monotonic clock (in fractional seconds):", end)
print("Time elapsed during the process:", end - start)


Output:

At the beginning of the process
Value of the monotonic clock (in fractional seconds): 216491.25
1
2
3
4
5
6
7
8
9
10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

At the end of the process
Value of the monotonic clock (in fractional seconds): 216516.875
Time elapsed during the process: 25.625


Last Updated : 08 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads