Map遍历的时间比较
2016-04-14 08:43
176 查看
map的四种遍历方法,点此查看
具体测试代码如下:
下面查看四种遍历消耗1000000条数据的消耗时间,测试方法用到的工具包是Freebencher
个人博客:http://www.whereta.com
具体测试代码如下:
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
相关文章推荐
- 第三周项目4—委派任务
- 第七周项目(2)-友员类增加年月日
- 053(十三)
- 杭电 FatMouse' Trade
- 扩展TextView之深入浅出实战篇
- 成员函数友员函数与一般函数有区别(1)外部
- C++单向链表之链表的创建
- algrothm_古典问题:兔子(斐波那契)
- 单向链表理解
- 飛飛(二十四)成员函数,友元函数,和一般函数的区别
- 日期在本年中的第几天
- iOS程序员须知
- 4000 Android中Activity相关知识-1
- Java中hashCode的作用
- 第四周项目五(3)-用递归方法求解
- 五:二叉树中和为某一直的路径
- 利用xml设置点击效果
- ios 一个正则表达式测试(只可输入中文、字母和数字)
- Caffe专业术语
- 第三周项目:换分币