Given a np.array A
and a function f
, I would like to create a tuple containing f(A)
. I simply did:
x = tuple(map(f,A))
Now I would like to improve the overall performance of my program and I think that maybe this part can be done faster. Intuitively, map
passes the array once and tuple
passes it a second time so perhaps there is a way to do it in one pass? Also, I want x
to be a tuple to use it as an array index. So if you know any faster way to index, feel free to share.
I would like to point out that this isn't a duplicate of Most efficient way to map function over numpy array since I want the final result to be a tuple and not a numpy array. So, using the accepted method and then converting to a tuple isn't necessarily the fastest way.
Some time data:
x = tuple(map(f,A))
takes ~13 secsx = tuple(np.fromiter((f(x) for x in A), int))
takes ~17 secsvf = np.vectorize(f)
,x = tuple(vf(A))
takes ~42 secs