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?
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.
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.
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.
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.
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.
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