E
E
Ernest Farukshin2020-04-06 23:56:57
Python
Ernest Farukshin, 2020-04-06 23:56:57

Why are NumPy arrays more efficient than Python lists?

Everywhere it is written that NumPy arrays are faster and more efficient than lists. Why is this so, do they have some more optimal implementation, or do I write this way, due to the fact that arrays have many useful and convenient functions that are efficiently written?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmshar, 2020-04-07
@Ernest3

Arrays in Numpy are implemented in much the same way as in C++. The main difference is that it is always a continuous piece of data of the same type in RAM . Due to these two fundamental properties and, accordingly, the lack of appropriate checks and transformations, operations on elements of Numpy arrays are significantly faster than on lists, especially with large amounts of stored information. The disadvantage of arrays in Numpy is the inefficient use of RAM in cases where the actual elements of the array are less than the declared size of the array.
Lists are implemented as a slightly modified C++ reference structure. More precisely, memory is allocated there in blocks, access is not directly to the element, but through a system of links, etc. In addition, lists are elements of different types, moreover, it is also possible to list lists or lists of other composite data types. In general, in order to perform a simple operation, the interpreter needs to perform quite a lot of checks and jumps.
In recent versions, the implementation of lists has been heavily optimized. However, operations on its elements remain slower than on elements of Numpy arrays.
The presence of "many useful and convenient functions" is already a derivative of the basic differences.

L
longclaps, 2020-04-07
@longclaps

Lists can be made up of anything,
arrays are made up of homogeneous elements.
Here is the whole secret.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question