Improve performance of gpsnow()
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 thegpstime
class as it's not needed and adds some overhead. - Add
as_unix
andas_gps
methods to theLeapData
class with an LRU cache so thatgpsnow
,unix2gps
andgps2unix
can reference the cached versions. The cache is invalidated whenever the leapsecond data needs to be updated. - Add a
data
property toLeapData
. 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
andLeapData.as_gps
inunix2gps
andgps2unix
, respectively, so that we can quickly bisect the leapsecond data and grab the right number of leapseconds to use.