Research on. Net collection classes-Ordered Set (2)-SortedDictionary & amp; lt; TKey, TValue &

Source: Internet
Author: User

From the class name, we can see that SortedDictionary <TKey, TValue> is an ordered set like SortedList described in the previous article. However, there is a big difference between the two in terms of the storage structure inside the class, sortedList is saved in an array and can only be regarded as an ordered linear table. The internal structure of SortedDictionary <TKey, TValue> is a red-black tree.

There are many good articles about the red and black trees in the garden, which have been thoroughly analyzed. So here we will not discuss the structure principle of the red and black trees. What are the differences between SortedDictionary and SortedList? When should I choose to use SortedDictionary?

The internal structure of SortedDictionary is the red-black tree, and the red-black tree is a balanced binary tree. SortedList is an ordered linear table and the internal structure is an Array. The binary search method is used to improve the efficiency. From the time complexity of the search, insert, and delete operations, they are all O (LogN), but the difference in the internal structure leads to performance differences in actual operations.

Performance Comparison Between SortedDictionary and SortedList -- insert
Because SortedList is saved as an array, each insert operation uses the Binary Search Method to locate the corresponding location. After the location is obtained, SortedList will move the values after the location to another location in sequence, empty the current BIT and insert the value. Array is used in this process. copy method, and calling this method is relatively performance-consuming. The Code is as follows:

Private void Insert (int index, TKey key, TValue value)
{
......

If (index <this. _ size)
{
Array. Copy (this. keys, index, this. keys, index + 1, this. _ size-index );
Array. Copy (this. values, index, this. values, index + 1, this. _ size-index );
}

......
}
When adding SortedDictionary, it only rotates nodes based on the characteristics of the red and black trees to maintain a balance and does not call Array. Copy.

Now let's test it with data: loop a random array of int type and capacity of 100000, and add it with SortedList and SortedDictionary respectively. (CodeTimer class in the Code, from Lao Zhao's article .)

Public void SortedAddInTest ()
{
Random random = new Random ();
Int array_count = 100000;
List <int> intList = new List <int> ();
For (int I = 0; I <= array_count; I ++)
{
Int ran = random. Next ();
IntList. Add (ran );
}

SortedList <int, int> sortedlist_int = new SortedList <int, int> ();
SortedDictionary <int, int> dic_int = new SortedDictionary <int, int> ();
CodeTimer. Time ("sortedList_Add_int", 1, () =>
{
Foreach (var item in intList)
{
If (sortedlist_int.ContainsKey (item) = false)
Sortedlist_int.Add (item, item );
}
});
CodeTimer. Time ("sortedDictionary_Add_int", 1, () =>
{
Foreach (var item in intList)
{
If (dic_int.ContainsKey (item) = false)
Dic_int.Add (item, item );
}
});
}
The results are as follows:

SortedList_Add_int
Time Elapsed: 4,311 ms
CPU Cycles: 8,249,183,130
Gen0: 0
Gen1: 0
Gen2: 0

SortedDictionary_Add_int
Time Elapsed: 217 ms
CPU Cycles: 278,164,530
Gen0: 1
Gen1: 1
Gen2: 0

It can be seen that SortedDictionary has better performance than SortedList in the case of a large number of addition operations.

Performance Comparison Between SortedDictionary and SortedList -- Query
In both query operations, the time complexity is O (LogN), and no additional operations in the source code cause performance loss, so what is their performance in query operations? Continue with the previous example for testing.

Public void SortedAddInTest ()
{
......

CodeTimer. Time ("sortedList_Search_int", 1, () =>
{
Foreach (var item in intList)
{
Sortedlist_int.ContainsKey (item );
}
});
CodeTimer. Time ("sortedDictionary_Search_int", 1, () =>
{
Foreach (var item in intList)
{
Dic_int.ContainsKey (item );
}
});
}
Result:

SortedList_Search
Time Elapsed: 602 ms
CPU Cycles: 1,156,460,630
Gen0: 0
Gen1: 0
Gen2: 0

SortedDictionary_Search
Time Elapsed: 667 ms
CPU Cycles: 1,256,685,950
Gen0: 0
Gen1: 0
Gen2: 0

It can be concluded that the query operation performance of the two queries is slightly different from that of the other query operations within dozens of milliseconds after 10 million cycles.

Performance Comparison Between SortedDictionary and SortedList -- delete
From the example of adding operations, we can see that because SortedList uses arrays to store data internally, and the limitations of the Array itself make most of the adding operations of SortedList call Array. the Copy method causes performance loss, which also exists in the delete operation.

Each delete operation of SortedList moves the value after the delete position one by one to fill in the blank of the delete position. This process is just the opposite of the add operation, and you also need to call Array. copy method. The related code is as follows.

Public void RemoveAt (int index)
{
......

If (index <this. _ size)
{
Array. Copy (this. keys, index + 1, this. keys, index, this. _ size-index );
Array. Copy (this. values, index + 1, this. values, index, this. _ size-index );
}

......
}
This is the same as the add operation. Therefore, we can predict that SortedDictionary performs better than SortedList in the case of a large number of delete operations.

Let's continue the test code above to verify this.

Public void SortedDictionaryTest ()
{
//.......

CodeTimer. Time ("sortedList_Delete_String", 1, () =>
{
Foreach (var item in temp_List)
{
Sortedlist. Remove (item );
}
});

CodeTimer. Time ("sortedDictionary_Delete_String", 1, () =>
{
Foreach (var item in temp_List)
& Nb

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.