I get a big array (image with 12 Mpix) in the array format from the python standard lib. Since I want to perform operations on those array, I wish to convert it to a numpy array. I tried the following:

import numpy
import array
from datetime import datetime
test = array.array('d', [0]*12000000)
t = datetime.now()
numpy.array(test)
print datetime.now() - t

I get a result between one or two seconds: equivalent to a loop in python.

Is there a more efficient way of doing this conversion?

link|improve this question

What is the actual source of your data? Does it have to come through the array type? – thouis Apr 15 '11 at 9:48
My source is a lib I cannot modify. I can't change it to use numpy. – Simon Apr 15 '11 at 9:57
feedback

1 Answer

up vote 6 down vote accepted
np.array(test)                                       # 1.19s

np.fromiter(test, dtype=np.int)                      # 1.08s

np.frombuffer(test)                                  # 459ns !!!
link|improve this answer
thank you! I was thinking of something like frombuffer. – Simon Apr 15 '11 at 10:08
dang, I didn't know about frombuffer! Thanks! – Garrett Berg Apr 15 '11 at 17:46
Is there anything else that counts as a 'buffer'? All numpy says is "An object that exposes the buffer interface." Are there any downfals to using this, and if not why doesn't np.array use it internally? – Garrett Berg Apr 15 '11 at 17:48
@Garett, yes there are: python buffers – Henry Gomersall Apr 16 '11 at 19:05
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.