Python | time.monotonic() method
Last Updated :
08 Oct, 2021
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
import time
value = time.monotonic()
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
import time
start = time.monotonic()
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 :
arr[i] = int ( input ())
i = i + 1
print (arr)
end = time.monotonic()
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
Share your thoughts in the comments
Please Login to comment...