what is the difference between time.perf_counter() and time.process_time()?
time.perf_counter()
keeps going during sleep, time.process_time()
does not.
time.perf_counter() → float
Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
time.process_time() → float
Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process. It does not include time elapsed during sleep. It is process-wide by definition. The reference point of the returned value is undefined, so that only the difference between the results of consecutive calls is valid.
See the official documentation
import time
def pc():
start = time.perf_counter()
time.sleep(1)
print(time.perf_counter()-start)
def pt():
start = time.process_time()
time.sleep(1)
print(time.process_time()-start)
pc() # 0.99872320449432
pt() # 0.0
perf_counter() should measure the real amount of time for a process to take, as if you used a stop watch.
process_time() will give you the time spent by the computer for the current process, a computer with an OS usually won’t spend 100% of the time on any given process. This counter shouldn’t count the time the cpu is running anything else.
Most cases perf_counter is probably preferable but process_time can be useful if you want to compare code efficiency.