简体   繁体   中英

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.

My question is: How caching an ArrayList works in Java? Are there cases, when an ArrayList implemented Queue or Stack perform better then a LinkedList implemented in Java?

hen an ArrayList implemented Queue or Stack perform better then a LinkedList implemented in Java?

When the queue is almost always empty or with 1 element, ArrayList is faster and creates less garbage (ie none)

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. Under these conditions ArrayList is faster as it doesn't create any objects or produce garbage.

BTW ArrayDeque is a better choice because this performs well even if there is more than 1 element.

How caching an ArrayList works in Java?

There is nothing special about the way the CPU caches accesses to an ArrayList.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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