• 并发list性能对比


    并发list性能对比

    代码演示:

    package com.dwz.concurrent;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.ConcurrentLinkedQueue;
    import java.util.concurrent.CopyOnWriteArrayList;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.atomic.AtomicInteger;
    
    public class ConcurrentListPerformanceTest {
        static class Entry{
            int threshold;//线程数
            long ms;//运行的平均时间
            public Entry(int threshold, long ms) {
                this.threshold = threshold;
                this.ms = ms;
            }
            
            @Override
            public String toString() {
                return "Count:" + threshold + ",ms:" + ms;
            }
        }
        
        private final static Map<String, List<Entry>> summary = new HashMap<>() ;
        
        public static void pressureTest(final Collection<String> list, int threshold) throws InterruptedException {
            System.out.println("Start pressure testing the map ["+list.getClass()+"] use the threshold ["+threshold+"]");
            long totalTime = 0L;
            final int MAX_THRESHOLD = 50000;
            for(int i = 0; i < 5; i++) {
                final AtomicInteger counter = new AtomicInteger(0);
                long startTime = System.nanoTime();
                ExecutorService executorService = Executors.newFixedThreadPool(threshold);
                
                for(int j = 0; j <= threshold; j++) {
                    executorService.execute(() -> {
                        for(int x = 0; x < MAX_THRESHOLD && counter.getAndIncrement() < MAX_THRESHOLD; x++) {
                            Integer randomNumber = (int) Math.ceil(Math.random() * 600000);
                            list.add(String.valueOf(randomNumber));
                        }
                    });
                }
                
                executorService.shutdown();
                executorService.awaitTermination(2, TimeUnit.HOURS);
                long endTime = System.nanoTime();
                long period = (endTime - startTime) / 1000000L;
                System.out.println(MAX_THRESHOLD + " element added in " + period + " ms.");
                
                totalTime += period;
            }
            
            List<Entry> entries = summary.get(list.getClass().getSimpleName());
            if (entries == null) {
                entries = new ArrayList<>();
                summary.put(list.getClass().getSimpleName(), entries);
            }
            entries.add(new Entry(threshold, (totalTime/5)));
            System.out.println("For the map ["+list.getClass()+"] the average time is " + (totalTime/5) + " ms.");
        }
        
        public static void main(String[] args) throws InterruptedException {
            for(int i = 10; i <= 100;) {
                pressureTest(new ConcurrentLinkedQueue<>(), i);
                pressureTest(new CopyOnWriteArrayList<>(), i);
                pressureTest(Collections.synchronizedList(new ArrayList<>()), i);
                i += 10;
            }
            
            summary.forEach((k, v) -> {
                System.out.println(k);
                v.forEach(System.out::println);
                System.out.println("=============================");
            });
        }
    }

    执行结果:

    SynchronizedRandomAccessList
    Count:10,ms:17
    Count:20,ms:18
    Count:30,ms:19
    Count:40,ms:44
    Count:50,ms:25
    Count:60,ms:27
    Count:70,ms:22
    Count:80,ms:32
    Count:90,ms:53
    Count:100,ms:29
    =============================
    ConcurrentLinkedQueue
    Count:10,ms:32
    Count:20,ms:15
    Count:30,ms:16
    Count:40,ms:77
    Count:50,ms:37
    Count:60,ms:29
    Count:70,ms:52
    Count:80,ms:20
    Count:90,ms:22
    Count:100,ms:38
    =============================
    CopyOnWriteArrayList
    Count:10,ms:6017
    Count:20,ms:6619
    Count:30,ms:7577
    Count:40,ms:8506
    Count:50,ms:8007
    Count:60,ms:7811
    Count:70,ms:7067
    Count:80,ms:6442
    Count:90,ms:6684
    Count:100,ms:7048
    =============================
    结果表明CopyOnWriteArrayList在插入数据的情况下速度确实很低
  • 相关阅读:
    2-3-4 tree留坑
    CCPC final Cockroaches
    对拍模板
    使用cronolog按日期分割日志
    linux git 命了
    变量加减乘除运算
    根据pom标签修改
    根据符号获取字符
    shell循环字符串数组
    git ssh key配置
  • 原文地址:https://www.cnblogs.com/zheaven/p/13992571.html
Copyright © 2020-2023  润新知