If you are creating a 1d array in Python is there any benefit to using the NumPy package?
As the array size increase, Numpy gets around 30 times faster than Python List. Because the Numpy array is densely packed in memory due to its homogeneous type, it also frees the memory faster.
NumPy uses much less memory to store data The NumPy arrays takes significantly less amount of memory as compared to python lists. It also provides a mechanism of specifying the data types of the contents, which allows further optimisation of the code. If this difference seems intimidating then prepare to have more.
array is just a convenience function to create an ndarray ; it is not a class itself. You can also create an array using numpy. ndarray , but it is not the recommended way. From the docstring of numpy.
It all depends on what you plan to do with the array. If all you're doing is creating arrays of simple data types and doing I/O, the array module will do just fine.
If, on the other hand, you want to do any kind of numerical calculations, the array module doesn't provide any help with that. NumPy (and SciPy) give you a wide variety of operations between arrays and special functions that are useful not only for scientific work but for things like advanced image manipulation or in general anything where you need to perform efficient calculations with large amounts of data.
Numpy is also much more flexible, e.g. it supports arrays of any type of Python objects, and is also able to interact "natively" with your own objects if they conform to the array interface.
Small bootstrapping for the benefit of whoever might find this useful (following the excellent answer by @dF.):
import numpy as np from array import array # Fixed size numpy array def np_fixed(n): q = np.empty(n) for i in range(n): q[i] = i return q # Resize with np.resize def np_class_resize(isize, n): q = np.empty(isize) for i in range(n): if i>=q.shape[0]: q = np.resize(q, q.shape[0]*2) q[i] = i return q # Resize with the numpy.array method def np_method_resize(isize, n): q = np.empty(isize) for i in range(n): if i>=q.shape[0]: q.resize(q.shape[0]*2) q[i] = i return q # Array.array append def arr(n): q = array('d') for i in range(n): q.append(i) return q isize = 1000 n = 10000000
The output gives:
%timeit -r 10 a = np_fixed(n) %timeit -r 10 a = np_class_resize(isize, n) %timeit -r 10 a = np_method_resize(isize, n) %timeit -r 10 a = arr(n) 1 loop, best of 10: 868 ms per loop 1 loop, best of 10: 2.03 s per loop 1 loop, best of 10: 2.02 s per loop 1 loop, best of 10: 1.89 s per loop
It seems that array.array is slightly faster and the 'api' saves you some hassle, but if you need more than just storing doubles then numpy.resize is not a bad choice after all (if used correctly).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With