Map遍历的时间比较
博客专区 > 清尘V 的博客 > 博客详情
Map遍历的时间比较
清尘V 发表于2年前
Map遍历的时间比较
  • 发表于 2年前
  • 阅读 66
  • 收藏 3
  • 点赞 1
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: map遍历的四种方法时间对比

map的四种遍历方法,点此查看

具体测试代码如下:

    int num = 1000000;

    final Map<Integer, String> map = new HashMap<Integer, String>(num);

    System.out.println("map设值");
    for (int i = 0; i < num; i++) {
        map.put(i, UUID.randomUUID().toString());
    }

    System.out.println("entrySet方法:可以同时获取key和value,推荐这种方法");

    FbJobResult result = Freebencher.benchmark(new FbTarget() { //the behavior
                                                   @Override
                                                   public boolean invoke() {
                                                       Set<Map.Entry<Integer, String>> entrySet = map.entrySet();

                                                       for (Map.Entry<Integer, String> entry : entrySet) {
                                                           Integer key = entry.getKey();
                                                           String value = entry.getValue();

                                                       }
                                                       return true;
                                                   }
                                               }, 10, // concurrency,
            10 // number of tests to run
    );
    System.out.println("-------entrySet方法时间----------");
    System.out.println(result.report());
    System.out.println("*****************************************");


    System.out.println("keySet方法:先获取key,再获取value");
    final Set<Integer> keySet = map.keySet();

    result = Freebencher.benchmark(new FbTarget() { //the behavior
                                       @Override
                                       public boolean invoke() {
                                           for (Integer key : keySet) {
                                               String value = map.get(key);
                                           }
                                           return true;
                                       }
                                   }, 10, // concurrency,
            10 // number of tests to run
    );
    System.out.println("-------keySet方法时间----------");
    System.out.println(result.report());
    System.out.println("*****************************************");


    System.out.println("values方法:只获取value");

    result = Freebencher.benchmark(new FbTarget() { //the behavior
                                       @Override
                                       public boolean invoke() {
                                           Collection<String> values = map.values();
                                           for (String value : values) {
                                           }
                                           return true;
                                       }
                                   }, 10, // concurrency,
            10 // number of tests to run
    );
    System.out.println("-------values方法时间----------");
    System.out.println(result.report());
    System.out.println("*****************************************");


    System.out.println("entrySet.iterator方法:迭代器");

    result = Freebencher.benchmark(new FbTarget() { //the behavior
                                       @Override
                                       public boolean invoke() {
                                           Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
                                           while (iterator.hasNext()) {
                                               Map.Entry<Integer, String> next = iterator.next();
                                               Integer key = next.getKey();
                                               String value = next.getValue();

                                           }

                                           return true;
                                       }
                                   }, 10, // concurrency,
            10 // number of tests to run
    );
    System.out.println("-------entrySet.iterator方法时间----------");
    System.out.println(result.report());
    System.out.println("*****************************************");


下面查看四种遍历消耗1000000条数据的消耗时间,测试方法用到的工具包是Freebencher

map设值
entrySet方法:可以同时获取key和value,推荐这种方法
Test started.
Awaiting termination...
Test completed.
-------entrySet方法时间----------
Concurrency:             10
Time taken for tests:    112ms
Successful tests:        10
Failed tests:            0
Tests per second:        89.28571428571429
Mean time per test:      88.2ms
Percentage of the test finished within a certain time (ms)
50%:                     88
60%:                     93
70%:                     101
80%:                     104
90%:                     109
95%:                     110
98%:                     110
99%:                     110
100%:                    110

*****************************************
keySet方法:先获取key,再获取value
Test started.
Awaiting termination...
Test completed.
-------keySet方法时间----------
Concurrency:             10
Time taken for tests:    218ms
Successful tests:        10
Failed tests:            0
Tests per second:        45.87155963302752
Mean time per test:      182.5ms
Percentage of the test finished within a certain time (ms)
50%:                     183
60%:                     211
70%:                     214
80%:                     215
90%:                     215
95%:                     216
98%:                     216
99%:                     216
100%:                    216

*****************************************
values方法:只获取value
Test started.
Awaiting termination...
Test completed.
-------values方法时间----------
Concurrency:             10
Time taken for tests:    123ms
Successful tests:        10
Failed tests:            0
Tests per second:        81.30081300813008
Mean time per test:      96.2ms
Percentage of the test finished within a certain time (ms)
50%:                     105
60%:                     107
70%:                     109
80%:                     111
90%:                     113
95%:                     115
98%:                     115
99%:                     115
100%:                    115

*****************************************
entrySet.iterator方法:迭代器
Test started.
Awaiting termination...
Test completed.
-------entrySet.iterator方法时间----------
Concurrency:             10
Time taken for tests:    103ms
Successful tests:        10
Failed tests:            0
Tests per second:        97.0873786407767
Mean time per test:      82.6ms
Percentage of the test finished within a certain time (ms)
50%:                     83
60%:                     90
70%:                     91
80%:                     94
90%:                     97
95%:                     103
98%:                     103
99%:                     103
100%:                    103

*****************************************


个人博客:http://www.whereta.com

共有 人打赏支持
粉丝 43
博文 107
码字总数 47780
×
清尘V
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: