您的位置:首页 > 其它

Map遍历的时间比较

2016-04-14 08:43 176 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: