time.perf_counter() or time.process_time() for performance measurements?

Viewed 2776

I understand that time.perf_counter() measures the total time elapsed, even while the process is currently not running. time.process_time() however only measures the time the process is actually running.

If I am just measuring the performance of a function, which of these two is preferred?

Since I am not actually interested in the time my CPU spends tending to other processes, I naturally thought time.process_time() would be the better (and more stable across different runs?) choice, but the name time.perf_counter() seems to suggest otherwise.

Code Example

import time
from tqdm import trange

start_time_proc = time.process_time()
start_time_perf = time.perf_counter()

tmp = False
for _ in trange(10_000_000):
    tmp = not tmp

elapsed_time_proc = time.process_time() - start_time_proc
elapsed_time_perf = time.perf_counter() - start_time_perf

print("process_time:", elapsed_time_proc)
print("perf_counter:", elapsed_time_perf)

https://repl.it/repls/GigaSpryScientists#main.py

1 Answers

The usual way to measure is to use perf counter and repeat the measurement a couple of times.

Process time is not ofently used and I can easily show you why: time.sleep(1) will take 0 seconds according to process time. Every time the process is removed from the scheduler, even if it's due to the code being tested, the process clock won't advance.

May I suggest you to take a look at the built-in timeit module? It uses perf counter as well, and may provide a more comfortable interface for repeated timing.

Edit:

If and only if, your function is entirely CPU bound, does not access external resources, nor cause any system call whatsoever, process_time is the more accurate and better choice.

Related