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')
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')
输出: