Comparison of traversal speed of several sets and comparison of several sets
Perform a simple test speed on the set operation. The data volume is 20 w. Compare the testing time of map, list, set, array, and queue.
First paste an introduction to these sets:
1.1 Set Interface1.2 List interface 1.3 Queue interface 1.4 Map interface
The following is the test code:
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; @ Beforepublic void init () {array = new String [leng]; set = new HashSet <String> (); list = new ArrayList <String> (); queue = new queue list <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 @ Testpublic void testArray () {Long startTime = new Date (). getTime (); for (String sk: array) {//} Long endTime = new Date (). getTime (); Long times = endTime-startTime; System. out. println ("time:" + times) ;}// list @ Testpublic void testList () {Long startTime = new Date (). getTime (); for (String sk: list) {//} Long endTime = new Date (). getTime (); Long times = endTime-startTime; System. out. println ("time:" + times) ;}// map @ Testpublic 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 ("time:" + 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 ("time 1:" + times1);} // Queue @ Testpublic void testQueue () {Long startTime = new Date (). getTime (); for (String s: queue) {//} Long endTime = new Date (). getTime (); Long times = endTime-startTime; System. out. println ("time 1:" + times);} // Set @ Testpublic void testSet () {Long startTime = new Date (). getTime (); for (String s: set) {//} Long endTime = new Date (). getTime (); Long times = endTime-startTime; System. out. println ("time:" + times );}
Time: array: 4 ms, list: 17 ms, map: 14 ms, 13 ms, queue: 15 ms, set: 14 ms
Data is slightly different from each test, but the difference is not big. The biggest difference is array and other, which is the fastest traversal and the slowest list.
References: http://www.cnblogs.com/nayitian/archive/2013/03/08/2950730.html