DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • LLMops: The Future of AI Model Management
  • Implement Hibernate Second-Level Cache With NCache
  • Transforming Customer Feedback With Automation of Summaries and Labels Using TAG and RAG
  • Supporting Offline Mode: Key Strategies and Benefits for iOS Mobile Apps

Trending

  • AI-Based Threat Detection in Cloud Security
  • How Trustworthy Is Big Data?
  • Streamlining Event Data in Event-Driven Ansible
  • AI, ML, and Data Science: Shaping the Future of Automation
  1. DZone
  2. Data Engineering
  3. Data
  4. Performance of Array vs. Linked-List on Modern Computers

Performance of Array vs. Linked-List on Modern Computers

Linked-list might have been universally better than array at one point, but modern tech has since improved and array may be preferred.

By 
Dat Hoang user avatar
Dat Hoang
·
Apr. 11, 18 · Analysis
Likes (5)
Comment
Save
Tweet
Share
34.6K Views

Join the DZone community and get the full member experience.

Join For Free

A Few Notes

The array mentioned here is an automatically-resized 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 from this post (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 an automatically-reallocated array in C++.

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

The CPU speed on modern computers is too fast, much faster than RAM-access speed. It would take about several hundred CPU cycles to read from RAM, which is way too slow. If that's the case, then most of the time, the CPU just sits there, drinks tea, and waits for its best friend—RAM.

And Then, CPU Caches Come into Play

The RAM-access speed is so 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 are most likely to be needed by the CPU in the near future. Sometimes, the data that the CPU needs to access next is not already in L1 cache (nor in L2 or L3 cache), and it must be fetched from RAM, then the CPU will have to wait for several hundred 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 the 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 the CPU is placed right next to each other, we can make use of locality of reference and reduce cache miss, which might cause huge performance overhead if it 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 lot of cache miss (since we can't make use of locality of reference), and introduce lots of performance overheads.

Performance of Array vs Linked-List

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

However, when it comes to a list of small elements (list of POD type, or list of pointers), the cost of moving elements around is chea, 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), the 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-lists "usual" 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, such as 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/structure and need to do a lot 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 an object in the heap, instead of a list of objects, because reference types 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

  • [1] vector vs list performance benchmark, Baptiste Wicht: http://baptiste-wicht.com/posts/2012/11/cpp-benchmark-vector-vs-list.html
  • [2] C++ vector, list, deque performance benchmark, Baptiste Wicht: http://baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html
  • [3] Locality of reference: https://en.wikipedia.org/wiki/Locality_of_reference
  • [4] Understanding CPU caching and performance, Jon Stokes: http://arstechnica.com/gadgets/2002/07/caching/
Data structure Data (computing) Cache (computing) Computer

Opinions expressed by DZone contributors are their own.

Related

  • LLMops: The Future of AI Model Management
  • Implement Hibernate Second-Level Cache With NCache
  • Transforming Customer Feedback With Automation of Summaries and Labels Using TAG and RAG
  • Supporting Offline Mode: Key Strategies and Benefits for iOS Mobile Apps

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!