Comparison of traversal speed of several sets and comparison of several sets

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.