What is best way to check execution time? I just use the perf_counter
from time module.
from time import sleep, perf_counter
start = perf_counter()
sleep(1)
finish = perf_counter()
if round(finish-start, 2) > 0:
print(f"Finsished in {round(finish-start, 2)} (s)")
Most people mean CPU time when they say "execution time" and for that you would use time.process_time()
. In your case, however, where all you are doing is sleeping between two consecutive calls, it would produce for the second call a return value of approximately 0. If you want elapsed time and are rounding to 2 places, I wouldn't think it would matter if you are using time.time
or time.perf_counter
. But perf_counter
provides the higher resolution when you do care about that.
import time
t0 = time.time()
t0a = time.perf_counter()
t0b = time.process_time()
time.sleep(1)
t1 = time.time()
t1a = time.perf_counter()
t1b = time.process_time()
print(round(t1 - t0, 2), round(t1a - t0a, 2), t1b - t0b)
Prints:
1.0 1.0 0.015625
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.