timeless-timing-attacks icon indicating copy to clipboard operation
timeless-timing-attacks copied to clipboard

Use `time.perf_counter_ns()` instead of `time.time_ns()`

Open sueskind opened this issue 1 year ago • 0 comments

First, thank you for this great project!

However, I fear, that there is some issue with the time measurement. Using time.get_clock_info() we can get this information:

>>> time.get_clock_info("time")
namespace(implementation='clock_gettime(CLOCK_REALTIME)', monotonic=False, adjustable=True, resolution=1e-09)
>>> time.get_clock_info("perf_counter")
namespace(implementation='clock_gettime(CLOCK_MONOTONIC)', monotonic=True, adjustable=False, resolution=1e-09)
  • time.time() and therefore time.time_ns() make no claim for being monotonic, meaning that it is possible that a later reading returns a lower number.
  • The resolution may vary, up to only being 16 milliseconds, as reported in this Stack Overflow post.

Note: This is platform dependent, in this case run in Python 3.11.5 on Linux 6.5.6-arch2-1.

I would suggest switching time.time_ns() with time.perf_counter_ns() as is usually done for reliable performance measurements.

There is a PR open fixing this at #5.

sueskind avatar Oct 09 '23 05:10 sueskind