perf_counter in Python

In order to time our code with high precision, much higher than time.clock() (that is going to be deleted in python 3.8) we can use perf_counter() and perf_counter_ns()

from time import perf_counter_ns

t1_start = perf_counter_ns()

<code here>

t1_end = perf_counter_ns()

print("Elapsed time is {t1_end - t1_start} ns")


  • perf_counter() is more precise than time.clock()