+

Python | time.monotonic () method

time.monotonic() The time unit in Python is used to get the value of a monotonic clock. Monotonous clock — it is a clock that cannot move backward. 
Since there is no checkpoint for the return value of monotonic hours, only the difference between the results of successive calls is allowed.

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.

Code # 1: Using the time.monotonic() method to get the monotonic clock value

# Python program to explain the time.monotonic () method

 
# time module import

import time

 
# Get the value
# monotonous hours using
# time.monotonic () method

value = time.monotonic ()

 
# output the value
# monotonous hours

print ( "Value of the monotonic clock (in fractional seconds):" , value)

Exit:

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

Code # 2: Using the time.monotonic() method to measure the elapsed time in long process.

# Python program to explain the time.monotonic () method

 
# time module import

import time

 
# Get the value
# monotonous hours on
# start the process code>
# using the time.monotonic () method

start = time.monotonic ()

 
# print the value
# monotonous hours

print ( "At the beginning of the process " )

print ( "Value of the monotonic clock (in fractional seconds):" , start)

 

i = 0

arr = [ 0 ] * 10

while i & lt;  10 :

# Accept user input

arr [i] = int ( input ())

i = i + 1

 
# Print user input

print (arr)

 
# Get the value
# monotonous hours
# using met ode time.monotonic ()

end = time.monotonic ()

 
# output the value
# monotonous hours

print ( "At the end of the process" )

print ( "Value of the monotonic clock (in fractional seconds):" , end)

print ( "Time elapsed during the process: " , end - start) 

Exit:

 At the beginning of the process Value of the monotonic c lock (in fractional seconds): 3957.237067598 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): 3966.407866892 Time elapsed during the process (in fractional seconds): 9.170799294000062 

Link: https://docs.python.org/3/library/time.html#time.monotonic

Get Solution for free from DataCamp guru