Skip to content

Improve performance of gpsnow()

Patrick Godwin requested to merge patrick.godwin/gpstime:gpsnow_perf into master

The short story is that I'm using gpsnow() in some low latency code which is calling this many times a second, so saw an opportunity to improve some of the performance of these calls, cutting down the runtime by about 35%.

Before:

%timeit gpstime.gpsnow()
12.4 µs ± 644 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

After:

%timeit gpstime.gpsnow()
8.04 µs ± 332 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

The changes are as follows:

  • Remove the custom __new__ definition in the gpstime class as it's not needed and adds some overhead.
  • Add as_unix and as_gps methods to the LeapData class with an LRU cache so that gpsnow, unix2gps and gps2unix can reference the cached versions. The cache is invalidated whenever the leapsecond data needs to be updated.
  • Add a data property to LeapData. This is really only done to check that the leapsecond data has been loaded properly in various locations whenever it's used.
  • Make use of LeapData.as_unix and LeapData.as_gps in unix2gps and gps2unix, respectively, so that we can quickly bisect the leapsecond data and grab the right number of leapseconds to use.

Merge request reports