I'm studying algorithms and data structures at the moment.
I thought I would run a quick timeit.timeit test for iterating through a list of 2**30 random integers in a list() comparing to the same for the array.array format.
I was expecting the array to finish first as one of the few muted benefits I have seen on other posts with a Python array is performance (I was initially wrongly under the impression that the list was implemented as a linked list: thank you for the correction Duncan)
Surely an array should be at least as quick as a list?
import os
import array
l = list(os.urandom(2**30))
a = array.array('I', l)
def test_list():
for i in l:
pass
def test_array():
for i in a:
pass
>>> timeit.timeit(test_array, number=5)
50.08525877200009
>>> timeit.timeit(test_list, number=5)
37.00491460799958
Here's my platform information: Python 3.6.5, [GCC 7.3.0] on linux x86_64 (Intel i5 4660)
test_arrayandtest_listdo, or would you like to tell us? And no, Python lists are not linked lists. I don't know where you got that from but they are stored internally as an array.