-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Iterating over a SkyCoord object is very slow #3323
Copy link
Copy link
Closed
Labels
Description
I was surprised to see how long it takes to iterate over a multi-valued SkyCoord object.
For example, it takes 19s to iterate over a SkyCoord object that contains 1000 coordinates:
In [1]: import numpy as np
In [2]: from astropy import units as u
In [3]: from astropy.coordinates import SkyCoord
In [4]: ra = np.random.uniform(0, 360, size=1000) * u.deg
In [5]: dec = np.random.uniform(-90, 90, size=1000) * u.deg
In [6]: crd = SkyCoord(ra, dec)
In [7]: timeit crd[500]
100 loops, best of 3: 19 ms per loop
In [8]: timeit [c for c in crd]
1 loops, best of 3: 19.4 s per loopIn contrast, iterating over a list of 1000 SkyCoord objects is roughly 500,000x faster:
In [9]: crd2 = [SkyCoord(ra[i], dec[i]) for i in range(len(ra))]
In [10]: %timeit crd2[500]
10000000 loops, best of 3: 38.7 ns per loop
In [11]: %timeit [c for c in crd2]
10000 loops, best of 3: 28.6 µs per loopMight there be room to increase the performance of accessing a SkyCoord item? Iterating over a list of coordinates seems like a common task to me.
Reactions are currently unavailable