LinkedBlockingQueue性能测试
2014-04-30 21:44
288 查看
LinkedBlockingQueue官方文档有这样的介绍:
Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.
在大多数并发应用中,链表队列一般比基于数组的队列有更高的吞吐率但是可预测性差。
有点违背我的直觉。
链表队列在插入新数据的时候,需要临时分配堆内存,应该要慢才对,可为什么反而要比预先分配好空间的数组队列性能高呢?
Google了一下 ,发现了别人的测试,竟然和官方文档相反。最后一条评论对我有点启发。
应该比较1读1写 ,而后再测试多读多写, 如果超过8读8写意义就更加不大了,估计你的机器是不会配备如此多的cpu可以同时执行那么多线程,从而造成线程切换时间的开销远大于本身队列的性能
先思考了一下,链表队列在读的时候,需要修改头指针;而基于数组的队列在读的时候,需要取模运算,这也许是性能差别的原因。
测试1个写线程,1个读线程,读写10000个随机数。
ArrayBlockingQueue耗时:50ms
LinkedBlockingQueue耗时:36ms
测试2个写线程,1个读线程,读满20000个数停止。(4核CPU)
ArrayBlockingQueue耗时:61ms
LinkedBlockingQueue耗时:38ms
关于性能测试,stackoverflow上有另一个相关的问题,涉及到了与性能相关的另一个维度,就是队列容量。
原文链接:http://stackoverflow.com/questions/5102570/implementation-of-blockingqueue-what-are-the-differences-between-synchronousque
这个测试中包含了限定容量的队列的性能对比,但是没有测试不限制容量的队列,通过在代码中增加不限容量的LinkedBlockingQueue的测试,发现
不限容量的LinkedBlockingQueue的吞吐量 > ArrayBlockingQueue > 限定容量的LinkedBlockingQueue。
Linked queues typically have higher throughput than array-based queues but less predictable performance in most concurrent applications.
在大多数并发应用中,链表队列一般比基于数组的队列有更高的吞吐率但是可预测性差。
有点违背我的直觉。
链表队列在插入新数据的时候,需要临时分配堆内存,应该要慢才对,可为什么反而要比预先分配好空间的数组队列性能高呢?
Google了一下 ,发现了别人的测试,竟然和官方文档相反。最后一条评论对我有点启发。
应该比较1读1写 ,而后再测试多读多写, 如果超过8读8写意义就更加不大了,估计你的机器是不会配备如此多的cpu可以同时执行那么多线程,从而造成线程切换时间的开销远大于本身队列的性能
先思考了一下,链表队列在读的时候,需要修改头指针;而基于数组的队列在读的时候,需要取模运算,这也许是性能差别的原因。
测试1个写线程,1个读线程,读写10000个随机数。
ArrayBlockingQueue耗时:50ms
LinkedBlockingQueue耗时:36ms
测试2个写线程,1个读线程,读满20000个数停止。(4核CPU)
ArrayBlockingQueue耗时:61ms
LinkedBlockingQueue耗时:38ms
关于性能测试,stackoverflow上有另一个相关的问题,涉及到了与性能相关的另一个维度,就是队列容量。
原文链接:http://stackoverflow.com/questions/5102570/implementation-of-blockingqueue-what-are-the-differences-between-synchronousque
这个测试中包含了限定容量的队列的性能对比,但是没有测试不限制容量的队列,通过在代码中增加不限容量的LinkedBlockingQueue的测试,发现
不限容量的LinkedBlockingQueue的吞吐量 > ArrayBlockingQueue > 限定容量的LinkedBlockingQueue。
相关文章推荐
- Linked List Cycle II
- LeetCode--No.203--Remove Linked List Elements
- Jackson转换泛型List出现错误java.util.LinkedHashMap cannot be cast to com.xxx
- 解释一下ArrayList Vector和LinkedList的实现和区别
- Linked List Circle题解
- 203. Remove Linked List Elements
- Leetcode_141_Linked List Cycle
- [leetcode] Linked List Cycle
- Java中Vector和ArrayList以及LinkedList的区别
- 提取不重复的整数——HashSet,TreeSet和LinkedHashSet的区别
- Reversing Linked List (25)
- Java LinkedList 实现原理
- LeetCode-Intersection of Two Linked Lists
- 203. Remove Linked List Elements
- leetcode_141_Linked List Cycle
- 深入分析LinkedBlockingQueue
- LeetCode Linked List Cycle & Linked List Cycle II
- Java中各种常用数据结构的使用(Vector、Stack、LinkedList)
- leetcode[114]Flatten Binary Tree to Linked List
- Java ArrayList 和 LinkedList 理解