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


Python time.process_time_ns()用法及代碼示例


Python中的時間模塊提供了各種與時間相關的函數。該模塊屬於Python的標準實用程序模塊。

time.process_time_ns()Python中的時間模塊方法用於獲取當前進程的係統和用戶CPU時間的總和(以納秒為單位)。此方法不包括睡眠期間經過的時間。此方法類似於time.process_time()方法,該方法以秒為單位返回當前進程的係統和用戶CPU時間之和。

由於未定義單調時鍾返回值的參考點,因此僅連續調用的結果之間的差有效。


用法: time.process_time_ns()

參數:不需要任何參數。

返回類型:此方法返回一個整數值,該整數值表示當前進程的係統和用戶CPU時間的總和(以納秒為單位)。

代碼1:用於time.process_time_ns()方法

# Python program to explain time.process_time_ns() method 
  
# importing time module 
import time 
  
# assigning n = 100   
n = 100 
    
# Get the sum of the system  
# and user CPU time of 
# the current process in nanoseconds 
# using time.process_time_ns() method 
start = time.process_time_ns()   
    
print("At the beginning of the process") 
print("Process Time (in nanoseconds):", start, "\n") 
# Here process time means sum of the system 
# and user CPU time of the current process 
    
  
# Print all natural numbers 
# from 1 to 100 
  
for i in range(1, n + 1):  
    print(i, end =' ')  
    
print()   
    
  
end = time.process_time_ns()  
     
print("\nAt the end of the process") 
print("Process time (in nanoseconds):", end)      
print("Elapsed time during the process (in nanoseconds):", end - start)  
輸出:
At the beginning of the process
Process Time (in nanoseconds): 31873819

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 

At the end of the process
Process time (in nanoseconds): 32271699
Elapsed time during the whole process (in nanoseconds): 397880

代碼2:為了表明time.process_time_ns()方法不包括睡眠期間經過的時間

# Python program to explain time.process_time_ns() method 
  
# importing time module 
import time 
  
  
# Get the sum of the system  
# and user CPU time of 
# the current process in nanoseconds 
# using time.process_time_ns() method 
start = time.process_time_ns()   
    
print("At the beginning of first example") 
print("Process Time (in nanoseconds):", start, "\n") 
# Here process time means sum of the system 
# and user CPU time of the current process 
    
# Print all natural numbers 
# from 1 to 50 
  
# assigning n = 50   
n = 50
    
for i in range(1, n + 1):  
    print(i, end =' ')  
      
  
print()   
  
end = time.process_time_ns() 
  
print("\nAt the end of the first example") 
print("Process time (in nanoseconds):", end)     
print("Elapsed time during the first example (in nanoseconds):", end-start)  
  
  
# Get the sum of the system  
# and user CPU time of 
# the current process in nanoseconds 
# using time.process_time_ns() method 
start = time.process_time_ns()   
    
print("\nAt the beginning of second example") 
print("Process Time (in nanoseconds):", start, "\n") 
# Here process time means sum of the system 
# and user CPU time of the current process 
    
# Print all natural numbers 
# from 1 to 50 
  
# assigning n = 100   
n = 50
    
for i in range(1, n + 1):  
    print(i, end =' ')  
      
  
print()   
  
# suspend the execution of the current 
# process for 10 seconds 
time.sleep(10) 
  
end = time.process_time_ns() 
print("\nAt the end of the second example") 
print("Process time (in nanoseconds):", end)     
print("Elapsed time during the second example (in nanoseconds):", end-start)  
  
  
  
# In both the examples  
# we can see (in the output below) 
# that elapsed time  
# is more or less the same  
# so, the suspension of process for 
# 10 seconds is not included
輸出:
At the beginning of first example
Process Time (in nanoseconds): 26901160

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 

At the end of the first example
Process time (in nanoseconds): 27091390
Elapsed time during the first example (in nanoseconds): 190230

At the beginning of second example
Process Time (in nanoseconds): 27186972

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 

At the end of the second example
Process time (in nanoseconds): 27377123
Elapsed time during the second example (in nanoseconds): 190151

參考: https://docs.python.org/3/library/time.html#time.process_time_ns



相關用法


注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | time.process_time_ns() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。