Python: What is the time.perf_counter method's time reference point?

Viewed 50

I'm trying to play with the time module in Python 3.9.7 on MacOS this is my script:

import time
 
print(time.perf_counter(), '\n', time.localtime(time.perf_counter()))
print('-----')
print(time.asctime(time.localtime(time.process_time())))

This is the output:

820.324263708 
 time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=1, tm_min=13, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)
-----
Thu Jan  1 01:00:02 1970

I understand Unix time concept, but it counts from Jan 1st 1970 to what? Definitely not "now". Any hints?

1 Answers

Per the documentation:

Return the value (in fractional seconds) of a performance counter ...

The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid.

Therefore, a call unto its own, is meaningless, as you've discovered. However, a call such as the following has more meaning with regard to code execution time.

import time

def perf():
    s = time.perf_counter()
    for _ in range(1000000):
        pass
    time.sleep(1)
    e = time.perf_counter()
    return e - s

>>> perf()
1.049831447
Related