El módulo de tiempo en Python proporciona varias funciones relacionadas con el tiempo. Este módulo se incluye en los módulos de utilidad estándar de Python.
time.monotonic_ns()
El método del módulo de tiempo en Python se usa para obtener el valor de un reloj monotónico en nanosegundos. Este método es similar al time.monotonic()
método que devuelve el valor del reloj monótono en fracciones de segundo. Un reloj monótono es un reloj que no puede retroceder.
Sintaxis: tiempo.monotonic_ns()
Parámetro: No se requiere ningún parámetro.
Tipo de devolución: este método devuelve un valor entero que representa el valor de un reloj monótono en nanosegundos.
Código #1: uso del time.monotonic_ns()
método para obtener el valor de un reloj monotónico en nanosegundos
# 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)
Value of the monotonic clock (in fractional seconds): 13486.679399824 Value of the monotonic clock (in nanoseconds): 13486679402777
Código n.º 2: uso del time.monotonic_ns()
método para medir el tiempo transcurrido en un proceso de ejecución prolongada.
# 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 f # 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("Factorial 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)
At the beginning of the process Value of the monotonic clock (in nanoseconds): 14671301967243 Factorial of 0: 1 Factorial of 1: 1 Factorial of 2: 2 Factorial of 3: 6 Factorial of 4: 24 Factorial of 5: 120 Factorial of 6: 720 Factorial of 7: 5040 Factorial of 8: 40320 Factorial of 9: 362880 At the end of the process Value of the monotonic clock (in nanoseconds): 14671302231487 Time elapsed during the process: 264244
Referencia: https://docs.python.org/3/library/time.html#time.monotonic_ns