zoukankan      html  css  js  c++  java
  • JMH之CopyOnWriteArrayList与ConcurrentLinkedQueue的性能测试

    JMH之CopyOnWriteArrayList与ConcurrentLinkedQueue的性能测试

    测试代码

    @BenchmarkMode(Mode.Throughput)
    @OutputTimeUnit(TimeUnit.MICROSECONDS)
    @State(Scope.Benchmark)
    public class TestList_JMH {
    
        CopyOnWriteArrayList smallList = new CopyOnWriteArrayList();
        ConcurrentLinkedQueue smallQueue = new ConcurrentLinkedQueue();
    
        CopyOnWriteArrayList bigList = new CopyOnWriteArrayList();
        ConcurrentLinkedQueue bigQueue = new ConcurrentLinkedQueue();
    
        @Setup
        public void setup(){
            for (int i=0;i<10;i++){
                smallList.add(new Object());
                smallQueue.add(new Object());
            }
    
            for (int i=0;i<1000;i++){
                bigList.add(new Object());
                bigQueue.add(new Object());
            }
    
        }
    
        @Benchmark
        public void smallListGet(){
            smallList.get(0);
        }
    
        @Benchmark
        public void smallQueueGet(){
            smallQueue.peek();
        }
    
        @Benchmark
        public void smallListSize(){
            smallList.size();
        }
    
        @Benchmark
        public void smallQueueSize(){
            smallQueue.size();
        }
    
        @Benchmark
        public void smallListWrite(){
            smallList.add(new Object());
            smallList.remove(0);
        }
    
        @Benchmark
        public void smallQueueWrite(){
            smallQueue.add(new Object());
            smallQueue.remove(0);
        }
    
        @Benchmark
        public void bigListWrite(){
            bigList.add(new Object());
            bigList.remove(0);
        }
    
        @Benchmark
        public void bigQueueWrite(){
            bigQueue.add(new Object());
            bigQueue.remove(0);
        }
    
        public static void main(String[] args) {
            Options opt = new OptionsBuilder().include(TestList_JMH.class.getSimpleName())
                    .forks(4).build();
            try {
                new Runner(opt).run();
            } catch (RunnerException e) {
                e.printStackTrace();
            }
        }
    }

    测试结果

    Benchmark                      Mode  Cnt     Score     Error   Units
    TestList_JMH.bigListWrite     thrpt   20     1.145 ±   0.043  ops/us
    TestList_JMH.bigQueueWrite    thrpt   20     0.001 ±   0.001  ops/us
    TestList_JMH.smallListGet     thrpt   20  1162.087 ±  96.210  ops/us
    TestList_JMH.smallListSize    thrpt   20  1491.125 ± 105.829  ops/us
    TestList_JMH.smallListWrite   thrpt   20    12.135 ±   0.206  ops/us
    TestList_JMH.smallQueueGet    thrpt   20  1224.323 ±  50.976  ops/us
    TestList_JMH.smallQueueSize   thrpt   20   109.864 ±   1.828  ops/us
    TestList_JMH.smallQueueWrite  thrpt   20     0.001 ±   0.001  ops/us

    结论

    在并发条件下,写的性能远远低于读。
    对于CopyOnWriteArrayList来说,内部有1000个元素的时候,写的性能要低于只有10个元素的时候,但是依然优于ConcurrentLinkedQueue。 Get操作,两者的读取性能差不多。由于实现上的差异,CopyOnWriteArrayList的size()操作的性能要好于ConcurrentLinkedQueue。 所以,即便是有少量的写入,在并发场景下,复制的消耗依然要很小。在元素总量不大的时候,CopyOnWriteArrayList的性能要好于ConcurrentLinkedQueue。
  • 相关阅读:
    排序:归并排序
    错误编码 = 10022 错误消息 = SDK 组件 Qupaisdk 启动出错,错误消息为 [Qupaisdk], the android stack error message is Fail to start the plugin, which is caused by No implem
    关于百川趣拍64位的问题
    在 APK 中找不到对应的 securityguard***.so 文件或者 so 文件载入出错
    趣拍proguard配置
    android多国语言文件夹
    错误编码 = 17
    阿里百川趣拍接入指南
    Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536
    Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536
  • 原文地址:https://www.cnblogs.com/beanbag/p/12830514.html
Copyright © 2011-2022 走看看