简体   繁体   English

当使用 ArrayList 的 Stack 或 Queue 实现比在 Java 中实现的 LinkedList 提供更好、更快的性能时,是否有任何情况?

[英]Is there any case when a Stack or a Queue implementation with ArrayList gives better, faster performance then a LinkedList implemented in Java?

In this Question I read the following:在这个问题中,我阅读了以下内容:

If you're doing comparatively few operations, ie less than 1000 or so enqueue/dequeues in total, then an array would be faster because it is contiguous in memory.如果您执行的操作相对较少,即总共少于 1000 个左右的入队/出队,那么数组会更快,因为它在内存中是连续的。

My question is: How caching an ArrayList works in Java?我的问题是:在 Java 中缓存 ArrayList 是如何工作的? Are there cases, when an ArrayList implemented Queue or Stack perform better then a LinkedList implemented in Java?在某些情况下,ArrayList 实现的 Queue 或 Stack 比 Java 中实现的 LinkedList 性能更好吗?

hen an ArrayList implemented Queue or Stack perform better then a LinkedList implemented in Java?如果 ArrayList 实现的 Queue 或 Stack 比 Java 中实现的 LinkedList 性能更好?

When the queue is almost always empty or with 1 element, ArrayList is faster and creates less garbage (ie none)当队列几乎总是空的或只有 1 个元素时,ArrayList 速度更快并且产生的垃圾更少(即没有)

If you have a fast consumer, it will almost always consumer what ever the producer produces as it produces it.如果你有一个快速的消费者,它几乎总是消费生产者生产的东西。 This means there is typically 0 or 1 elements in the queue.这意味着队列中通常有 0 或 1 个元素。 Under these conditions ArrayList is faster as it doesn't create any objects or produce garbage.在这些条件下 ArrayList 更快,因为它不会创建任何对象或产生垃圾。

BTW ArrayDeque is a better choice because this performs well even if there is more than 1 element.顺便说一句,ArrayDeque 是更好的选择,因为即使元素超过 1 个,它也能很好地执行。

How caching an ArrayList works in Java?在 Java 中缓存 ArrayList 是如何工作的?

There is nothing special about the way the CPU caches accesses to an ArrayList. CPU 缓存访问 ArrayList 的方式没有什么特别之处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM