Performance Comparison of multiple maps in Java (TreeMap, HashMap, ConcurrentSkipListMap)

Source: Internet
Author: User

Problem

Comparison of Java Native

1. TreeMap

2. HashMap

3. ConcurrentSkipListMap

Efficiency of three Map types.

Result

Simulate the insertion and search of massive data within, and test the performance by adding and searching. The results are as follows:


Map type Insert Search (within million data records)
  10 W 50 W 100 million 150 million 0-1 W 0-25 W 0-50 W
ConcurrentSkipListMap 62 MS 227 MS 433 MS 689 ms 7 MS 80 MS 119 MS
HashMap 18 MS 93 MS 217 MS 303 ms 2 MS 13 MS 45 MS
TreeMap MS 33 228 MS 429 MS 584 MS 4 ms MS 34 61 MS

 


Analysis description


 

Figure 1-1 example of comparison between constant and logn Function efficiency (horizontal axis-n data volume, vertical axis-f (n) Time)


TreeMap is implemented based on the red/black tree (a self-balancing Binary Search Tree), and the average time complexity can reach O (log n ).
HashMap is implemented based on the hash, and the average time complexity can reach O (1 ).
ConcurrentSkipListMap is implemented based on the jump table, and the average time complexity can reach O (log n ).

:
When the amount of data increases, HashMap will cause hash conflicts, which takes some time to resolve conflicts. Therefore, f (n) = 1 floats upwards.
As the amount of data increases, HashMap takes less time and is stable. In a single-threaded environment, HashMap has a great advantage over TreeMap and ConcurrentSkipListMap in insertion and search.

(1) Comparison between TreeMap and HashMap

The key-value pairs stored in HashMap are random when they are retrieved. It stores data based on the key's HashCode value and can directly obtain its value based on the key, with fast access speed. HashMap is the best choice for inserting, deleting, and locating elements in a Map.

Ø TreeMap obtains the sorted key-value pairs. The maintenance of the balance between insertion and deletion sacrifices some efficiency. However, it would be better for TreeMap to traverse keys in natural or custom order.

The HashMap function is added and searched in this test, which is more efficient than TreeMap.


(2) Comparison between TreeMap and ConcurrentSkipListMap

The Skip list (Skip table) is a data structure that can replace the Balance Tree. By default, it is in ascending order of Key values. The Skip list distributes sorted data in a multi-layer link table. It uses a 0-1 random number to determine whether a data is going up or down. It is an algorithm of "space for time, A forward pointer is added to each node. during insertion, deletion, and search, some impossible nodes can be ignored, thus improving the efficiency.
Probability-based data structure balancing is much simpler than display-based data structure balancing. For most applications, using Skip list is easier than using Tree algorithms. Because the Skip list is relatively simple, it is easier to implement it. Although it has the same time complexity as the Balance Tree (O (logn), the constant items of the skip list are much smaller. Skip list also saves space. On average, a node only needs 1.333 pointers (or even fewer ).


 

Figure 1-2 Skip list structure (taking sequence 7, 14, 85 as an example)

Skip listNature

(1) A level is composed of multiple layers, which are randomly generated by a certain probability.
(2) Each layer is an ordered linked list. By default, it is in ascending order. You can alsoComparatorSort, depending on the construction method used.
(3) The Bottom-layer (Level 1) linked list contains all elements.
(4) If an element appears in the Level I linked list, it also appears in the linked list under Level I.
(5) Each node contains two pointers, one pointing to the next element in the same linked list and the other pointing to the following element.

Ø ConcurrentSkipListMap is of the Skip list nature and is suitable for concurrent access to large-scale data. Multiple Threads can safely and concurrently perform insert, remove, update, and access operations. Compared with other data structures with lock mechanisms, the data structure is advantageous under great pressure.

Ø when inserting data in TreeMap, the Balance Tree is strictly rotated (for example, the balance tree has a left-hand rotation) to ensure the balance. Therefore, the Skip list is easier to implement and has a higher running efficiency than the Balance Tree.
The added function in this test shows that the efficiency of ConcurrentSkipListMap and TreeMap is not much different.

The TreeMap function is more efficient after 50 million data records, because the ConcurrentSkipListMap self-locking mechanism takes some efficiency, but for multi-threaded Concurrent Environments, ConcurrentSkipListMap is more efficient than Treep.

The get method of map is used in this test. For ConcurrentSkipListMap, The subMap () method can be used to obtain sequential fragments. It takes only 1 ms to extract 50 million sub-sequences, which has great advantages. ConcurrentSkipListMap is more efficient than HashMap and TreeMap.

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.