Python | time.monotonic_ns() method

Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules.

time.monotonic_ns() method of time module in Python is used to get the value of a monotonic clock in nanoseconds. This method is similar to time.monotonic() method which returns the monotonic clock value in fractional seconds. A monotonic clock is a clock that can not go backwards.

Syntax: time.monotonic_ns()



Parameter: No parameter is required.

Return type: This method returns an integer value which represents the value of a monotonic clock in nanoseconds.

Code #1: Use of time.monotonic_ns() method to get value of a monotonic clock in nanoseconds

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Value of the monotonic clock (in fractional seconds): 13486.679399824
Value of the monotonic clock (in nanoseconds): 13486679402777

Code #2: Use of time.monotonic_ns() 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_ns() method
  
# importing time module
import time
  
  
# Function to calculate factorial 
# of the given number
def factorial(n):
    f = 1
    for i in range(n, 1, -1):
        f = f * i
      
    return
  
  
# Get the value of the
# monotonic clock in nanoseconds at the 
# beginning of the process
# using time.monotonic_ns() method
start = time.monotonic_ns()
  
# print the value of 
# the monotonic clock in nanoseconds
print("At the beginning of the process")
print("Value of the monotonic clock (in nanoseconds):", start, "\n")
  
# Calculate factorial of all
# numbers form 0 to 9
i = 0
fact = [0] * 10;
  
while i < 10:
    fact[i] = factorial(i)
    i = i + 1
  
# Print the calculated factorial
for i in range(0, len(fact)):
    print("Factorail of % d:" % i, fact[i])
  
# Get the value of
# monotonic clock in nanoseconds
# using time.monotonic_ns() method
end = time.monotonic_ns()
  
# print the value of 
# the monotonic clock
print("\nAt the end of the process")
print("Value of the monotonic clock (in nanoseconds):", end)
print("Time elapsed during the process:", end - start)    

chevron_right


Output:

At the beginning of the process
Value of the monotonic clock (in nanoseconds): 14671301967243 

Factorail of 0: 1
Factorail of 1: 1
Factorail of 2: 2
Factorail of 3: 6
Factorail of 4: 24
Factorail of 5: 120
Factorail of 6: 720
Factorail of 7: 5040
Factorail of 8: 40320
Factorail of 9: 362880

At the end of the process
Value of the monotonic clock (in nanoseconds): 14671302231487
Time elapsed during the process: 264244

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



My Personal Notes arrow_drop_up

self motivated and passionate programmer

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.