Why NumPy arrays over standard library arrays?

Question:

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?

Asked By: john_science

||

Answers:

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.

Answered By: Midnighter

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)

Answered By: mgilson
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.