time.perf_counter_ns wraps around and becomes negative in windows after couple of minutes
time.perf_counter_ns wraps around and becomes negative in windows after couple of minutes. for me it happened around 16 minutes after the first call, but it depends on the precision of the system perf counter.
import time time.perf_counter_ns() time.sleep(16*60) time.perf_counter_ns() # this will be negative
the cause of this problem is the following line in
return space.newint(tolong(diff) * 10**9 // time_state.divisor)
the problem is that there is an int64 overflow in the multiplication.
diff is the number of ticks that elapsed from the first call, which can be very large.
imagine a tick is 1ns, then after a second the multiplication result will be 1e18, and after 10 seconds there will be an overflow.
the solution in my opinion is to use
r_longlonglong (128bits) instead of
r_int64 in this case, or just not casting