0

This Python script

import time

while True:
    ns = time.time_ns()
    print(ns)

prints lots of identical values (always non-decreasing though). I am using Python 3.10 and Windows 10. Looks like a clock precision issue. Is there a way to get strictly increasing values?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 2
    All the various options (`time.time()`, `time.time_ns()`, etc.) seems to be limited to Windows's ~16ms resolution. There may be some workable solutions here: [High-precision clock in Python](https://stackoverflow.com/q/1938048/13843268). – sj95126 Dec 08 '22 at 01:08

1 Answers1

0

This class seems to provide higher resolution timing:

import time

class Timer:
    def __init__(self):
        self._offset_ns = time.time_ns() - time.perf_counter_ns()

    def time_ns(self):
        return self._offset_ns + time.perf_counter_ns()