當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。