Several operating speed comparisons

Source: Internet
Author: User

The collection operation is carried out in a simple test speed, the data volume is 20w, and the map,list,set,array,queue is compared with the Traverse test time.

Paste an Introduction to these collections first:

1.1 Set Interface
    1. Each element that is stored in the set must be unique, and the set interface does not guarantee the order of the elements being maintained;
    2. HashSet class: In order to quickly find the set of the design, the object stored in HashSet must define HASHCODE (), which does not guarantee the iteration order of the set;
    3. Linkedhashset class: Has a hashset query speed, and internally uses the chain list to maintain the order of elements (the Order of insertions).
1.2 List interface
    1. The list saves objects in the order in which they are entered, and does not perform operations such as sorting;
    2. ArrayList class: A list implemented by an array that allows fast random access to elements, but inserts and removes elements from the list is slow;
    3. LinkedList class: Sequential access is optimized, and the cost of inserting and deleting to the list is small, and random access is relatively slow.
1.3 Queue Interface
    1. The queue is used to simulate the data structure of queues such as "FIFO". Typically, queues do not allow random access to elements in the queue.
    2. Arraydeque Class: The implementation class for the queue sub-interface deque, array way.
    3. LinkedList class: Is the implementation class of the list interface, and it also implements the Deque interface (queue sub-interface). So it can also be used as a double-ended queue, or as a "stack".
1.4 Map Interface
    1. Add, remove Operations Put/remove/putall/clear
    2. Query Operation Get/containskey/containsvalue/size/isempty
    3. View Operation Keyset/values/entryset
    4. The Map.entry interface (map's EntrySet () method returns a collection of objects that implement the Map.entry interface) Getkey/getvalue/setvalue

Here is the test code:

public static int leng = 200000;private string[] array;private set<string> set;private list<string> List;priv Ate queue<string> queue;private map<string, string> Map; @Beforepublic void init () {array = new String[leng];s ET = new hashset<string> (); list = new arraylist<string> (); queue = new linkedlist<string> (); map = new has Hmap<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 D Ate (). 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 D Ate (). 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:4ms,list:17ms,map:14ms,13ms,queue:15ms,set:14ms

Data based on each test a slight gap, but the difference is not small, array and other differences the largest, belong to the fastest traversal, list slowest.

Reference: http://www.cnblogs.com/nayitian/archive/2013/03/08/2950730.html

Several operating speed comparisons

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.