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')
輸出:
相關用法
- Python Wand function()用法及代碼示例
- Python Numbers choice()用法及代碼示例
- Python ord()用法及代碼示例
- Python sum()用法及代碼示例
注:本文由純淨天空篩選整理自manojkumarreddymallidi大神的英文原創作品 Python time.perf_counter_ns() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。