Skip to content Skip to sidebar Skip to footer

Is Linked List Always Slower Than Python List?

I'm working on Problem Solving with Algorithms and Data Structures and come across this question: Design and implement an experiment that will compare the performance of a Python l

Solution 1:

Other answers have skipped a very important detail, Linked list will outperform arrays in say remove() method only when the pointer to node to be deleted is provided as parameter, not the value of node.

Otherwise, you will have to search through list, which has same O(n) complexity as removing element from an indexed-based list.

But there is another slightly less important factor at play here. Python list is actually implemented in C. A pure Python program is unlikely to beat a C counterpart, specially when that is written and optimized by experts for many years.

Solution 2:

Python lists are implemented from arrays. So you are comparing Linked Lists to Arrays.

In Linked list you can insert/delete elements easily , but it takes more time in Arrays to move the other elements once the element is inserted/deleted.

Please refer comparison between array and linkedlist for more details. Also this quora question explains the implementation of list in python.

Solution 3:

Well your test doesn't test something where a linked list would have an advantage. Here's a test where it does:

>>>from timeit import timeit>>>linked_list = UnOrderedList()>>>timeit(lambda: linked_list.add(0), number=10**5)
0.08297442221169149

>>>python_list = []>>>timeit(lambda: python_list.insert(0, 0), number=10**5)
1.5988611595369093

Or you could just use your own test but without setting a to a random value (i.e., keep it at i-1):

Listtime: 0.0. LinkedListtime: 0.0Listtime: 0.00100016593933. LinkedListtime: 0.0Listtime: 0.00200009346008. LinkedListtime: 0.0Listtime: 0.00300002098083. LinkedListtime: 0.0Listtime: 0.00300002098083. LinkedListtime: 0.0Listtime: 0.00399994850159. LinkedListtime: 0.0Listtime: 0.00499987602234. LinkedListtime: 0.0Listtime: 0.00699996948242. LinkedListtime: 0.0Listtime: 0.00699996948242. LinkedListtime: 0.0Listtime: 0.00799989700317. LinkedListtime: 0.0Listtime: 0.00999999046326. LinkedListtime: 0.0Listtime: 0.00899982452393. LinkedListtime: 0.0
...

Solution 4:

Python's lists are based on C arrays. Arrays have the advantage over linked lists during iteration and random access, while linked lists have the advantage in random insertions and deletions.

This goes into detail of the theoretical differences betweent the two.

Solution 5:

Think it deserves a dedicated answer, in terms of removal by value.

First, fix your test

In your example, arrays are different. Your linked list is in reversed order (100, 99, ...), because list2.add(a) adds to the start.

Quick fix is to reverse Python range, when populating linked list: range(i-1,-1,-1). This way elements will be in the same order as Python list.

Test

Now, test removals from list end to start (i-1, i-2, ...). Array list is faster (Python list) most of the time, while linked list gets closer (and faster further) only within 1% length to the list start (a = round(i / 99)).

So even though Python list needs to shift all the items following the deleted item, it's still faster.

That's due to internal Python implementation optimizations. As well as memory optimizations that are possible due to array list nature - because elements are sequential in memory, you get all the benefits out of data locality.

So yes, removals around list start (very close to start) are faster. But then, if you need such removals, just build Python array in the reversed order at first, and then delete from end, instead. It will be faster (as you observed above).

P.S. There are probably Python libraries with better Linked List implementation, but still, most of the time array list is faster.

Post a Comment for "Is Linked List Always Slower Than Python List?"