当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python time.monotonic()用法及代码示例


Python中的时间模块提供了各种与时间相关的函数。

time.monotonic()Python中的时间模块方法用于获取单调时钟的值。单调时钟是不能向后移动的时钟。由于未定义单调时钟返回值的参考点,因此仅连续调用的结果之间的差有效。

用法: time.monotonic()

参数:不需要任何参数。

返回类型:此方法返回一个浮点值,该值表示小数秒内的单调时钟的值。

代码1:用于time.monotonic()获得单调时钟值的方法

# 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)
输出:
Value of the monotonic clock (in fractional seconds): 2116.429736057

代码2:用于time.monotonic()在长时间运行过程中测量经过时间的方法。

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

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



相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | time.monotonic() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。