When trying to measure how long a loop or another routine takes in Python, you’ll have the choice between multiple functions. The most common ones for task are process_time
and perf_counter
. But what’s the difference? And which one should you choose?
In Python, both time.process_time()
and time.perf_counter()
will return a float value (in seconds) for CPU time of the current process. However, time.process_time()
*will not include* the time elapsed during sleep, while time.perf_counter()
*will include* time elapsed during sleep.
Should You Use process_time() or perf_counter()?
- If you want to know the performance of a particular routine, use
time.process_time()
to make sure you measure only the CPU processing time, not the system stalls or sleep - If you want to measure how your whole system behaves (especially if you have IO tasks like accessing a web server or a file), use
time.perf_counter()
to measure the whole time it takes to access data and perform operations on it
What’s The Difference Between process_time() and process_time_ns()?
time.process_time()
and time.process_time_ns()
perform essentially the same measurement. The difference lies in the value they return.
- time.process_time() will return a value in seconds, as a float (available since Python 3.3)
- time.process_time_ns() will return a value in nanosecondes, as an int (available since Python 3.7)
The exact same distinction exists between time.perf_counter() and time.perf_counter_ns().
How To Use process_time() or perf_counter()?
As stated in the Python documentation,
The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.
which means to measure the performance of a routine, you’ll need to call each function twice – once before your routine start, and once your routine has finished executing.
Then you subtract the last call to the first call:
import time
test_list = list('ABCDE')
start = time.process_time()
for i in range (10_000_000):
t = ''.join(test_list)
stop = time.process_time()
print(stop - start)
>>> 1.9363144830000039
In this example, we’re joining a list of 5 elements 10 million times. The output is the time it took to perform the operation, in seconds.
You would the exact same syntax to measure the performance with time.process_time_ns()
, time.perf_counter()
or time.perf_counter_ns()
:
import time
test_list = list('ABCDE')
start = time.process_time_ns()
for i in range (10_000_000):
t = ''.join(test_list)
stop = time.process_time_ns()
print(stop - start)
start = time.perf_counter()
for i in range (10_000_000):
t = ''.join(test_list)
stop = time.perf_counter()
print(stop - start)
start = time.perf_counter_ns()
for i in range (10_000_000):
t = ''.join(test_list)
stop = time.perf_counter_ns()
print(stop - start)
>>> 1794464805
>>> 1.8189330770001106
>>> 1796737746
There are obviously other ways to convert a list to a string in Python – the goal here was simply to compare the different methods to measure performance in Python.