Python CPU Clock. time.clock() vs time.perf_counter() vs time.process_time()

Viewed 1345

I wanted to measure how long sections of my code run. I needed this to be deterministic so that I get the same duration every time (seconds/milliseconds/etc) regardless of what is happening in the background. So i wanted to measure CPU time using time.clock() on unix.

time.clock() has since been decrecated and its suggested to switch to either perf_counter or process_time. I was looking at the python doc and found the following.

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.

Since I am not using any form of sleep in my code, the primary difference seems to be 'process-wide' and 'system-wide'. Could sometime elaborate the difference?

Secondly, is this the correct way to do this?

1 Answers

Both perf_counter and timeit will give you the time that your block of code tested had taken to perform.

time.process_time() it does not and it calculates what the CPU have taken, which is not necessarily the same as the function or block of code.

I found this thread on Github, seems that the question is quite advanced and may be completely different depending on the OS or program to be benchmark-ed.

Something that time.process_time() is not counting is the Parent Multi-thread:

"One consequence of using time.process_time is that the time spent in child processes of the benchmark is not included. Multithreaded benchmarks also return the total CPU time counting all CPUs."

perf_counter

from time import perf_counter


start = perf_counter()

for _ in range(10000):
    x = "-".join(str(n) for n in range(100))
end = perf_counter()
print('Perf Counter= ', end-start)
# Perf Counter=  0.23170840000000004

timeit

import timeit

print(timeit.timeit('"-".join(str(n) for n in range(100))', number=10000))
# 0.20687929999999993
Related