featured image

Performance of array vs linked-list on modern computers

posted on Friday August 4th, 2017 / IN Uncategorized /

Few notes

The array mentioned here is automatically-reallocated array (vector in C++, ArrayList in Java or List in C#).

The benchmark graph between vector & list I showed in this post belongs to “Baptiste Wicht” in http://baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html (Thanks for saving my time for the benchmarking stuff).

From theory

People say Linked-list has much better performance than array when it comes to random-insertion & random-deletion. That’s what we’ve learn in theory, too.

And I’ve seen many people around the internet suggest to use linked-list when we need to do a lots of random-insertion & random-deletion.

To practice

“How could it be?”

“Impossible!”

Note: The “list” above is implemented as doubly linked-list in C++. And the “vector” is implemented as automatically-reallocated array in C++.

It’s not that what people say is not true. It was once true in theory, and in those old days – when the CPU speed was too slow. But now, the context has changed, and the architecture of computers have changed. Therefore, it might not be true anymore in some situations.

The CPU speed on modern computers are too fast, much faster than RAM-access speed. It would take about several hundreds of CPU cycles to read from RAM (Way too slow!!!). If that’s the case, then most of the time, what the CPU do is just to sit there, drink tea, and wait for its best friend – RAM.

And then, CPU caches come into play

The RAM-access speed is too slow that if CPU processes data directly on RAM, 99% of CPU speed will become a waste, and we can just make use of 1% of its power. Therefore, they invented CPU cache to solve this problem. The cache memory is very fast, and is integrated directly on CPU. Its access speed is near the CPU speed. So now, instead of accessing data directly on RAM, CPU will access data on RAM indirectly through L1 cache (There are usually three levels of caches, and L1 cache is the fastest among them).

However, the CPU cache does not solve the problem completely. Because memory cache size is much smaller than RAM. We still need RAM as our main memory. And the CPU caches will just hold small pieces of data that is most likely to be needed by the CPU in the near future. Sometimes, the data that the CPU need to access next is not already in L1 cache (not in L2 or L3 cache, too), and it must be fetched from RAM, then the CPU will have to wait for several hundreds cycles for the data to become available (This is what we call cache miss).

Therefore, to reduce cache miss, when the CPU wants to access data at address x in RAM, it will not only fetch the data at address x, but also neighborhood of address x. Because we assume “if a particular memory location is referenced at a particular time, then it is likely that nearby memory locations will be referenced in the near future.”[3] (This is what we call locality of reference). So, if the data to be processed by CPU is placed right next to each others, we can make use of locality of reference and reduce cache miss (Which might cause huge performance overhead if occurs often).

The problem of linked-list

Unlike array which is a cache-friendly data structure because its elements are placed right next to each other, elements of linked-list can be placed anywhere in the memory. So when iterating through linked-list, it will cause a lots of cache miss (since we can’t make use of locality of reference), and introduce a lots of performance overheads.

Performance of array vs linked list

When we perform random-insertion or random-deletion on array, the subsequent elements needs to be moved.

However, when it come to a list of small elements (list of POD type, or list of pointers), the cost of moving elements around is so cheap, and much cheaper than the cost of cache misses. Therefore, most of the time, when working with a list of small data types (whatever type of operations it is: from iterating, random-insertion, random deletion to number crunching), performance of array will be much better than linked-list.

But when we need to work with a list of large elements ( > 32 bytes), the cost of moving elements around grows up to be higher than the cost of linked-list’s “usually-happened” cache misses. Then the linked list will have better performance than array.

Conclusion

We should prefer array over linked-list when working with a list of small elements (a list of POD type, a list of pointers in C++ or a list of references in Java/C#).

And nearly most of the time, we will use array because:

  • In C++, we don’t often store a list of large data type, but a list of pointers that point to large data type. (If we do need to store a list of large-sized class/struct and need to do a lots of random-insertion and random-deletion, then linked-list would be a better choice)
  • In Java, we’re forced to use a list of references that point to object in the heap, instead of a list of objects, because reference type are placed on the heap by default and we can just make a reference to it. (Since reference is just a managed pointer, its size is no more than 8 bytes)
  • In C#, we often use class instead of struct, so we also often have to deal with a list of references that point to object in the heap. (And if we do use struct, we don’t often create large struct since creating struct that has large size is a bad practice in C#)

CPU cache plays a very important role in performance improvement of programs on modern computers.

References

    By Dat Hoang

    Tagged With