• 记录一次并没有什么用的对比测试


    一、假设有一个这样的数据对象需要不断的产生,销毁

    public class TestData {
      private String id;
      private String val;
    
      public String getId() {
        return id;
      }
    
      public void setId(String id) {
        this.id = id;
      }
    
      public String getVal() {
        return val;
      }
    
      public void setVal(String val) {
        this.val = val;
      }
    }

    二、构造一些那样的对象,分别存放在map和list中

    private ConcurrentMap<String, TestData> makeTestMap(int count) {
        ConcurrentMap<String, TestData> map = new ConcurrentHashMap<String, TestData>();
        for (int i = 0; i < count; i++) {
            TestData data = new TestData();
            data.setId(String.valueOf(i));
            data.setVal(UUID.randomUUID().toString());
            map.put(String.valueOf(i), data);
        }
        return map;
    }
    
    private List<TestData> makeTestList(int count) {
        List<TestData> list = new ArrayList<TestData>();
        for (int i = 0; i < count; i++) {
            TestData data = new TestData();
            data.setId(String.valueOf(i));
            data.setVal(UUID.randomUUID().toString());
            list.add(data);
        }
        return list;
    }

    三、从map和List中随机的取一个,并返回花费时间

    public long getMapTime(String posStr, Map<String, TestData> map) {
        long t = System.nanoTime();
        map.get(posStr).getVal();
        // System.out.println("map get!!! " + map.get(posStr).getVal());
        return System.nanoTime() - t;
    }
    
    public long getListTime(String posStr, List<TestData> list) {
        long t = System.nanoTime();
        for (TestData td : list) {
            if (posStr.equals(td.getId())) {
            td.getVal();
            // System.out.println("list get!!! " + td.getVal());
            break;
            }
        }
        return System.nanoTime() - t;
    }

    四、随机从map和List中分别取一百次测试数据的值,计算平均花费时长

    @Test
    public void testPerformance() {
        int CAPACITY = 50000;
        int TEST_COUNT = 100;
        ConcurrentMap<String, TestData> map = makeTestMap(CAPACITY);
        List<TestData> list = makeTestList(CAPACITY);
        long mapSum = 0L;
        long listSum = 0L;
        for (int i = 0; i < TEST_COUNT; i++) {
            int pos = new Random().nextInt(CAPACITY);
            String posStr = String.valueOf(pos);
            // System.out.println(i+".("+posStr+")");
            // System.out.println("MAP take - " + testMap(posStr, map) + " ns");
            // System.out.println("LIST take - " + testList(posStr, list) + " ns");
            mapSum += getMapTime(posStr, map);
            listSum += getListTime(posStr,list);
        }
        System.out.println("map average take "+ mapSum/100 + " ns");
        System.out.println("list average take "+ listSum/100 + " ns");
    }


    五、一些测试数据

    // 容器中共500条数据
    // map average take 2011 ns
    // list average take 109312 ns
    
    // 容器中共5000条数据
    // map average take 3020 ns
    // list average take 408248 ns
    
    // 容器中共50000条数据
    // map average take 3917 ns
    // list average take 1452222 ns
    
    // 容器中共500000条数据
    // map average take 7548 ns
    // list average take 9193437 ns
  • 相关阅读:
    [转]system函数返回值探究
    [转]bat中的特殊字符,以及需要在bat中当做字符如何处理
    [转]null和""以及==与equals的区别
    粘包问题
    并发编程
    GIL锁
    五种IO模型
    css选择器
    并发与串行
    模块(二)
  • 原文地址:https://www.cnblogs.com/yoyotl/p/8057053.html
Copyright © 2020-2023  润新知