标签:
对集合操作进行简单的进行测试速度,数据量20w,对map,list,set,array,queue进行遍历测试时间对比。
先粘贴一段对这些集合的介绍:
下面是测试代码:
public static int leng = 200000; private String[] array; private Set<String> set; private List<String> list; private Queue<String> queue; private Map<String, String> map; @Before public void init() { array = new String[leng]; set = new HashSet<String>(); list = new ArrayList<String>(); queue = new LinkedList<String>(); map = new HashMap<String, String>(); for (int i = 0; i < leng; i++) { String key = "didi:" + i; String value = "da"; array[i] = key; set.add(key); list.add(key); queue.add(key); map.put(key, value); } } // shzu @Test public void testArray() { Long startTime = new Date().getTime(); for (String sk : array) { /// } Long endTime = new Date().getTime(); Long times = endTime - startTime; System.out.println("时间:" + times); } // list @Test public void testList() { Long startTime = new Date().getTime(); for (String sk : list) { /// } Long endTime = new Date().getTime(); Long times = endTime - startTime; System.out.println("时间:" + times); } // map @Test public void testMap() { Long startTime = new Date().getTime(); for (Map.Entry<String, String> entry : map.entrySet()) { entry.getKey(); } Long endTime = new Date().getTime(); Long times = endTime - startTime; System.out.println("时间:" + times); Long startTime1 = new Date().getTime(); for (String key : map.keySet()) { String value = (String) map.get(key); } Long endTime1 = new Date().getTime(); Long times1 = endTime - startTime; System.out.println("时间1:" + times1); } // Queue @Test public void testQueue() { Long startTime = new Date().getTime(); for (String s: queue) { // } Long endTime = new Date().getTime(); Long times = endTime - startTime; System.out.println("时间1:" + times); } // Set @Test public void testSet() { Long startTime = new Date().getTime(); for (String s: set) { // } Long endTime = new Date().getTime(); Long times = endTime - startTime; System.out.println("时间:" + times); }
时间:array:4ms,list:17ms,map:14ms,13ms,queue:15ms,set:14ms
数据根据每次测试略有差距,但相差不大,array和其他差距最大,属于 最快的遍历,list最慢。
参考资料:http://www.cnblogs.com/nayitian/archive/2013/03/08/2950730.html
标签:
原文地址:http://www.cnblogs.com/jeyson/p/5371058.html