C # Set

Source: Internet
Author: User

StringbuilderIt turns out to be a collection based on a linked list rather than an array. Instead of increasing capacity by 2, it adds a stringbuilder node with the capacity of int num = math. max (minblockcharcount, math. min (this. length, 8000); minblockcharcount: the number of remaining characters after the last node is filled; this. length: Fill in the data added to the sum of the capacity of the above two nodes. Basically, the maximum capacity of each node is 8000 characters. After the length is full, the node will be added and no previous data will be copied. Stringbuilder has a field named m_chunkprevious pointing to the previous node. I don't know whether this is a new implementation method or previously,
The efficiency of append data is indeed faster, and the previous data will not be copied. Other operations may be difficult to implement, but the efficiency may be slightly affected. This implementation will greatly improve the efficiency of adding a large number of strings. If a continuous array is used to store strings, the performance will be somewhat affected when copying data.

 

StringFor strings, I think you like comparison +, + =, and string. format, String. concat has good performance with it. If it is just one or a few sequential statements, it doesn't make any sense to say who has good performance. It doesn't reflect who has good performance, A large amount of data tests are required to compare the performance. When the number of cycles is the same, their performance is almost the same. If it is necessary to compare the speed, "+"> "+ ="> "Concat"> "format" is not a common saying that "+ =" has better performance than "+" (not always this result ). In fact, to improve the performance, we need to reduce the number of cycles. In the same way, the performance is improved, basically because the performance is inversely proportional to the number of cycles, performance is similar. Let's take a look at their compilation.CodeAs we can see, the number of commands executed by + and + = is the same. Concat has fewer instructions to move pointer than format, and the number of other commands is the same, of course, their garbage collection times are the same. In short, it doesn't matter if a few strings use either of them. After all, it doesn't involve performance issues. I like to use format because it makes the Code better understand and looks more comfortable. A large number of strings must use stringbuilder.

 

Test code:
View code

   Static   Void  Test1 (){  Int Timer =100000  ;  Using ( New Operationtimer ( "  + =  "  )){  String S1 = String  . Empty;  For ( Int I = 0 ; I <timer; I ++) {S1 + = I. tostring ();}}  Using ( New Operationtimer ( "  +  "  )){  String S1 = String  . Empty;  For ( Int I =0 ; I <timer; I ++ ) {S1 = S1 + I. tostring ();}}  Using ( New Operationtimer ( "  Concat  "  )){  String S1 = String  . Empty;  For (Int I = 0 ; I <timer; I ++ ) {S1 = String  . Concat (S1, I. tostring ());}}  Using ( New Operationtimer ( "  Format  "  )){  String Format = " {0}  "  ;  String S1 = String  . Empty;  For ( Int I = 0 ; I <timer; I ++ ) {S1 = S1 + String  . Format (format, I. tostring ());}}}  Static  Void  Test2 (){  Int Timer = 110000  ;  Using ( New Operationtimer ( "  +  "  )){  String S1 = String  . Empty;  For ( Int I = 0 ; I <timer; I + = 2  ) {S1 = S1 + I. tostring () + (I + 1  ). Tostring ();}}  Using ( New Operationtimer ( "  + =  "  )){  String S1 =String  . Empty;  For ( Int I = 0 ; I <timer; I + = 2  ) {S1 + = "  I  " + I. tostring () + (I + 1  ). Tostring ();}}  Using (New Operationtimer ( "  Concat  "  )){  String S1 = String  . Empty;  For ( Int I = 0 ; I <timer; I + = 2  ) {S1 = String . Concat (S1, I. tostring (), (I + 1  ). Tostring ());}}  Using ( New Operationtimer ( "  Format  "  )){  String Format = "  {0} {1}  "  ;  String S1 =String  . Empty;  For ( Int I = 0 ; I <timer; I + = 2  ) {S1 = S1 + String . Format (format, I. tostring (), (I + 1  ). Tostring ());}}} 

 

The Assembly Code is as follows:

Test results:

 

ArraylistYou can discard it. Use List to add value-added data to arraylist. The data performance is not as good as list, and the reference type performance is similar. The Value Type arraylist will be packed and unpacked, and the performance is almost the same, while the reference type is called by pointer, so do not think that you are handsome, list generates a set of code for each value type, and the reference type is the same set of code. In fact, I didn't know why it was like this before. After reading the C ++ template, I realized that the template parameters can be of any type, A set of code is generated for each type, but C ++ has the template semi-special function, which adds a constraint to the template parameters. The parameters of this template must be pointer types, with these two templates, the value type will call the first template to generate a set of code, because each value type occupies different bytes, the pointer type calls the special template. Because each pointer occupies 4 bytes, you do not need to generate a set of code for each type. The Code expansion problem is solved in this way. The list capacity is basically 2 times the amplification, and then the previous data is copied, the value type directly copies the data, the reference type directly copies the reference of the object, if the object is modified, the references in the list are modified. Of course, the reference types are the same.

 

HashtableIt is out of date and replaced by dictionary. Non-generic sets will certainly be replaced by generics. I think their implementation principles should be similar. For hash dictionaries, I think we can just understand how they are hashed out. In fact, the methods for raising the total of each set are almost the same, only the most importantAlgorithmDifferent. The object has a gethashcode () method. This method can generate a different integer with a length of 10 for each object and then hash the number into an array, the array is expanded in the order of prime numbers. That is to say, the length of the array is a prime number, which is mainly used for uniform hash. If two identical keys are added to the dictionary, that is, they are hashed to the same position, and an exception occurs. The hashset is also based on the hash, when multiple objects are hashed to the same place, it does not run out of exceptions, but returns directly. It is a good choice to exclude duplicate data from one or more Arrays Using hashset.

 

StackStack is very simple. It is scaled up twice as much as an array is filled, from 0 ~ N-1, the end of the stack to add the elements in the stack to clear, that is, the first in the first out.

 

QueueThe queue is also very simple. By default, it is resized in the form of 2. The expansion multiples can be customized. When the queue is entered, it is equivalent to filling the array, from 0 ~ N-1, The out queue is to return an index of the elements known, each element out of the queue, this index is increased by 1, the elements in the out queue is cleared, the position occupied by the elements is retained, that is, the size of the array remains unchanged, but you cannot access it.

 

SortdictionaryIt is based on the red and black trees. This red and black tree is still difficult to understand and is still being learned. That is, the search speed is fast, and the time complexity is O (logn). When adding elements, the structure of the tree is often adjusted.

 

Author: Chen taihan

Blog: http://www.cnblogs.com/hlxs/

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.