ndou
ndou

Reputation: 1178

Convert numpy array to standard library array without memory allocation

Is there a way to convert a numpy ndarray (numpy.array) to a standard-library array (array.array) without reallocating the data?

For the record it is possible to convert an array.array to a ndarray using the buffer interface, so I hope the way round is possible:

import numpy
import array
a_std = array.array('d', [1, 2, 3])
a_np = numpy.ndarray(shape=(3, ), buffer=a_std, dtype='d')
a_np[0] = 666.
assert a_std[0] == 666.

Upvotes: 1

Views: 527

Answers (1)

ndou
ndou

Reputation: 1178

My best guess so far is that it is not possible: memory reallocation cannot be avoided.

The fastest way I have found to convert my numpy array to an array.array is to use ndarray.tobytes():

import numpy
import array
a_np = np.random.uniform(size=(10 * 1000 * 1000))  # 76 MiB
a_std = array.array('d', a_np.tobytes())

numpy.testing.assert_allclose(a_std, a_np)

Quick benchmark with other methods (using IPython):

%timeit a_std = array.array('d', a_np.tobytes())
10 loops, best of 3: 56.8 ms per loop

%timeit a_std = array.array('d', a_np.data)
1 loop, best of 3: 946 ms per loop

%timeit a_std = array.array('d', a_np)
1 loop, best of 3: 1.17 s per loop

Upvotes: 1

Related Questions