سؤال

If I only need 1D arrays, what are the performance and size-in-memory benefits of using NumPy arrays over Python standard library arrays? Or are there any?

Let's say I have arrays of at least thousands of elements, and I want: fast direct access-by-index times and I want the smallest memory footprint possible. Is there a performance benefit to using this:

from numpy import array
a = array([1,2,3,4,5])

over this:

from array import array
a = array('i', [1,2,3,4,5])

Standard Python lists would have fast access-by-index times, but any array implementation will have a much smaller memory footprint. What is a decent compromise solution?

هل كانت مفيدة؟

المحلول

numpy is great for its fancy indexing, broadcasting, masking, flexible view on data in memory, many of its numerical methods and more. If you just want a container to hold data, then use an array.array or why not even a simple list?

I suggest taking a look at the numpy tutorial.

نصائح أخرى

this depends entirely on what you're planning on doing with the array.

>>> from array import array
>>> a = array('i', [1,2,3,4,5])
>>> a + a
array('i', [1, 2, 3, 4, 5, 1, 2, 3, 4, 5])

Note that the standard lib treats an array much more like a sequence which might not be desireable (or maybe it is ... Only you can decide that)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top