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


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


Python time.perf_counter_ns() 函数以纳秒为单位给出时间的整数值。

用法

from time import perf_counter_ns

我们可以使用 start 和 stop 函数找到经过的时间。我们还可以通过减去 stoptime 和 starttime 来找到整个程序中经过的时间

范例1:

Python3




# import  perf_counter_ns()
from time import perf_counter_ns
  
# integer input from user, 2 input in single line
n, m = map(int, input().split())
  
# Start the stopwatch / counter
start = perf_counter_ns()
  
for i in range(n):
    t = int(input())  # user gave input n times
    if t % m == 0:
        print(t)
  
# Stop the stopwatch / counter
stop = perf_counter_ns()
  
print("Elapsed time:", stop, 'ns', start, 'ns')
  
print("Elapsed time during the whole program",
      stop-start, 'ns')

输出

范例2:

Python3


# import  perf_counter_ns()
from time import perf_counter_ns
  
# integer input from user, 2 input in single line
n, m = map(int, input().split())
  
# Start the stopwatch / counter
start = perf_counter_ns()
  
for i in range(n):
    t = int(input())  # user gave input n times
    if t % m == 0:
        print(t)
  
# Stop the stopwatch / counter
stop = perf_counter_ns()
  
print("Elapsed time:", stop, 'ns', start, 'ns')
  
print("Elapsed time during the whole program",
      stop-start, 'ns')

输出:




相关用法


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