Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it more efficient to create a small array, and then expand it, rather than to create an array entirely from a large list?

Tags:

Found the following code in a book but couldn't get the complete explanation.

x = array('d', [0] * 1000000)
x = array('d', [0]) * 1000000 

The python code in the first case is creating an array of 1000000 lengths while in the 2nd part is creating a single size array and multiplying the size by the same factor. The code in the 2nd case is 100 times faster than the first case.

What is the exact reason for the speed difference? How does python implementation of arrays play a role?

like image 575
Anshul Jain Avatar asked Nov 29 '18 06:11

Anshul Jain


People also ask

Why are arrays more efficient than lists?

An array is faster than a list in python since all the elements stored in an array are homogeneous i.e., they have the same data type whereas a list contains heterogeneous elements. Moreover, Python arrays are implemented in C which makes it a lot faster than lists that are built-in in Python itself.

Which is more efficient array or list?

The array is faster in case of access to an element while List is faster in case of adding/deleting an element from the collection.

What is the biggest benefit of using an array instead of individual variables and why?

One of the major advantages of an array is that they can be declared once and reused multiple times. It represents multiple values by making use of a single variable. This helps in the improvement of the reusability of code and also improves the readability of the code.

What is the advantage of using arrays over multiple variables?

1) Array stores data elements of the same data type. 2) Maintains multiple variable names using a single name. Arrays help to maintain large data under a single variable name. This avoid the confusion of using multiple variables.


1 Answers

A Python list stores Python objects, but an array.array object stores raw C data types.

The first line requires individually processing every object in [0] * 1000000, following pointers and performing type checking and dynamic dispatch and reference counting and all that a million times to process every element and convert its data to a raw C double. Every element happens to be the same, but the array constructor doesn't know that. There's also the expense of building and cleaning up the million-element list.

The second line is way simpler. Python can just memcpy the array's contents a million times.

like image 131
user2357112 supports Monica Avatar answered Oct 11 '22 16:10

user2357112 supports Monica