Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays vs Linked Lists in terms of locality

Say we have an unsorted array and linked list. The worst case when searching for an element for both data structures would be O( n ), but my question is:

Would the array still be way faster because of the use of spatial locality within the cache, or will the cache make use of branch locality allowing linked lists to be just as fast as any array ?

My understanding for an array is that if an element is accessed, that block of memory and many of the surrounding blocks are then brought into the cache allowing for much faster memory accesses.

My understanding for a linked list is that since the path that will be taken to traverse the list is predictable, then the cache will exploit that and still store the appropriate blocks of memory even though the nodes of the list can be far apart within the heap.

like image 927
Kacy Avatar asked Sep 28 '13 07:09

Kacy


People also ask

Do arrays have better cache locality than linked list?

Extra memory space for a pointer is required for each element of the list. Arrays have a better cache locality that can make a pretty big difference in performance.

Which is better to use array or linked list?

Better use of Memory: From a memory allocation point of view, linked lists are more efficient than arrays. Unlike arrays, the size for a linked list is not pre-defined, allowing the linked list to increase or decrease in size as the program runs.

When arrays are better than linked list give an example?

The linked list would be a better choice if the data part is larger in size. Suppose the data is of 16 bytes. The memory space occupied by the array would be 16*7=112 bytes while the linked list occupies 20*4=80, here we have specified 20 bytes as 16 bytes for the size of the data plus 4 bytes for the pointer variable.

Why is linked list preferred over array?

Linked lists are preferable over arrays when:you don't know how many items will be in the list. With arrays, you may need to re-declare and copy memory if the array grows too big. you don't need random access to any elements. you want to be able to insert items in the middle of the list (such as a priority queue)


1 Answers

Your understanding of the the array case is mostly correct. If an array is accessed sequentially, many processors will not only fetch the block containing the element, but will also prefetch subsequent blocks to minimize cycles spent waiting on cache misses. If you are using an Intel x86 processor, you can find details about this in the Intel x86 optimization manual. Also, if the array elements are small enough, loading a block containing an element means the next element is likely in the same block.

Unfortunately, for linked lists the pattern of loads is unpredictable from the processor's point of view. It doesn't know that when loading an element at address X that the next address is the contents of (X + 8).

As a concrete example, the sequence of load addresses for a sequential array access is nice and predictable. For example, 1000, 1016, 1032, 1064, etc.

For a linked list it will look like: 1000, 3048, 5040, 7888, etc. Very hard to predict the next address.

like image 126
Craig S. Anderson Avatar answered Oct 05 '22 10:10

Craig S. Anderson