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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。