List of objects to numpy array
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Realization
--
Chapters
00:00 List Of Objects To Numpy Array
01:43 Accepted Answer Score 0
02:24 Thank you
--
Full question
https://stackoverflow.com/questions/2886...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #opencv #imageprocessing #numpy
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Realization
--
Chapters
00:00 List Of Objects To Numpy Array
01:43 Accepted Answer Score 0
02:24 Thank you
--
Full question
https://stackoverflow.com/questions/2886...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #opencv #imageprocessing #numpy
#avk47
ACCEPTED ANSWER
Score 0
A trial case:
In [765]: class Keypoints(object):
def __init__(self):
self.pt=[1.,2.]
.....:
In [766]: keypoints=[Keypoints() for i in xrange(1000)]
In [767]: cd=np.array([k.pt for k in keypoints])
In [768]: cd
Out[768]:
array([[ 1., 2.],
[ 1., 2.],
[ 1., 2.],
...,
[ 1., 2.],
[ 1., 2.],
[ 1., 2.]])
In [769]: cd_x=cd[:,0]
In timeit tests, the keypoints step takes just as long as the cd calculation, 1ms.
But the 2 simpler iterations
cd_x=np.array([k.pt[0] for k in keypoints])
cd_y=np.array([k.pt[1] for k in keypoints])
takes half the time. I was expecting the single iteration to save time. But in these simple cases, the comprehension itself takes only half of the time, the rest is creating the array.
In [789]: timeit [k.pt[0] for k in keypoints]
10000 loops, best of 3: 136 us per loop
In [790]: timeit np.array([k.pt[0] for k in keypoints])
1000 loops, best of 3: 282 us per loop