Python | time.monotonic() method

Time module in Python provides various time related functions.

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

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: Use of time.monotonic() method to get value of a monotonic clock

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)    

chevron_right


Output:

At the beginning of the process 
Value of the monotonic clock (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

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.