📜  Python| time.monotonic() 方法

📅  最后修改于: 2022-05-13 01:55:33.695000             🧑  作者: Mango

Python| time.monotonic() 方法

Python time.monotonic()方法用于获取单调时钟的值。单调时钟是不能倒退的时钟。由于单调时钟的返回值的参考点未定义,因此只有连续调用的结果之间的差异才有效。

Python time.monotonic()方法语法:

Python时间单调()示例

示例 1:使用time.monotonic()方法获取单调时钟的值

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


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


输出:

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

示例 2:使用time.monotonic()方法测量长时间运行过程中的经过时间

Python3

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

输出:

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