C # string sorting

Source: Internet
Author: User

1. Resident Mechanism

Token from the instructor.

String operations are one of the most basic operations. If you give an article, there are many strings in it. to process such a large amount of data, we should optimize both space and time, in C #, the resident mechanism is implemented. As the name suggests, the resident mechanism is that the string stays somewhere, and this location is the memory space.

When we create a String object, we normally apply for a space for each string object to store this string. Once this object is created, it cannot be modified. We can only change the reference of this object, to change the string corresponding to this object, then the original space will be recycled. In this case, we can still accept the unmodifiable object. However, if we have many identical strings, each of them will occupy a certain amount of memory space, so we can think of an optimization idea, it is to point all the same points to a memory space, so we can look at the optimization of this problem.

String str1 = "xym ";

String str2 = "xym ";

String str3 = "xym ";

The optimization we think of is str2 = str1; str3 = str1; so the space of str2 and str3 is recycled, all pointing to the space of str1. It is precisely because it cannot be modified, str2 and str3 do not conflict with each other. This is actually the role of the resident mechanism, so that all the same strings share the same memory space, greatly saving space and the burden on the recycling mechanism, the document describes whether the string is in its internal hash table when an object is created. If it is in, it points to that part of the memory. If it is not in the hash table, it is inserted.

Here refer

1. The String object with the same character sequence will not be created again
2. the string resident mechanism is similar to the string literal + String Literal operation.
Iii. String resident wit is not suitable for variable + String Literal format
4. Call string. intern to force the operation result to reside.
5. Resident strings cannot be recycled by GC
6. String resident is based on the entire process

 

2. String Declaration

String mystring;

String mystring1 = string. empty;

String mystring2 = "";

Let's discuss the differences between the three.

 

Debug finds that the values assigned after the three declarations are different. mystring1 and mystring2 are the same, but because mystring does not give the initial value, the default value is null, note that there is a good protection mechanism in C #. When you use unassigned variables or objects, an error will be reported. Therefore, you must assign a value to mystring to use it, here I have a problem. If we can use string mystring = NULL at the beginning, how does the compiler determine whether we have initialized it? I think it is to determine the equal sign, no relevant information is found online.

Now let's see if the references of mystring1 and mystring2 are the same. They should be the same according to the resident mechanism.

static void Main(string[] args)        {            string Mystring ;            string Mystring1 = string.Empty;            string Mystring2 = "";           Console.WriteLine(object.ReferenceEquals(Mystring1, Mystring2));        }


The answer is true.

So this is consistent with our previous resident space, indicating that all strings are empty and are identical references. Therefore, we can think that mystring1 and mystring2 are equivalent, therefore, these two statements are equivalent.

 

3. Comparison Test

It is said to judge whether a string is null, using if (0 = mystring. length) is faster than if ("" = mystring). Let's do a comparison experiment.

The test I designed judges the test speed for 100000000, times. Here mystring = "", each test is 10 times and the average value is obtained.

The following is the time when the local machine runs if ("" = mystring ).


10000 cycles: 0.20002 Ms
100000 cycles: 0.90005 Ms
1000000 cycles: 7.30042 Ms
10000000 cycles: 71.90411 Ms
100000000 cycles: 714.44086 Ms
Press any key to continue...

Is the time to run if (0 = mystring. length)


10000 cycles: 0.20001 Ms
100000 cycles: 0.90005 Ms
1000000 cycles: 8.30048 Ms
10000000 cycles: 84.10482 Ms
100000000 cycles: 826.44727 Ms
Press any key to continue...



I run it multiple times and found that if ("" = mystring) is better than if (0 = mystring. length). Note that mystring = "" here. If we want mystring = "123456789", if (0 = mystring. length) Will the time be
Longer, the length function algorithm should be O (N). We guess the time will be longer, so let's test it again.

Is the time for running mystring = "123456789" If (0 = mystring. length)


10000 cycles: 0.20001 Ms
100000 cycles: 1.00006 Ms
1000000 cycles: 8.30047 Ms
10000000 cycles: 82.90474 Ms
100000000 cycles: 819.04684 Ms
Press any key to continue...



If ("" = mystring)


10000 cycles: 0.30002 Ms
100000 cycles: 1.30008 Ms
1000000 cycles: 9.30052 Ms
10000000 cycles: 95.00543 Ms
100000000 cycles: 923.65283 Ms
Press any key to continue...



The test results show that when mystring! = "", If (0 = mystring. length) is faster than if ("" = mystring). Here we can think about why this is the case. In fact, if the strings are equal, we first compare whether the two strings are equal in length, then compare them one by one.
= "", It is clear that if ("" = mystring) first calculates the length of the two strings and then compares them one by one. In this case, if ("" = mystring) calculate the length of the two strings. However, if the two strings point to the same memory, you can first determine whether the two strings are consistent during comparison. If they are consistent, they must be the same,

So we can do another experiment. If mystring = "123", then if ("123" = mystring) And if (3 = mystring. Length) are two faster ones.

First look at if ("123" = mystring)


10000 cycles: 0.20001 Ms
100000 cycles: 0.90005 Ms
1000000 cycles: 7.20041 Ms
10000000 cycles: 70.10401 Ms
100000000 cycles: 711.44069 Ms
Press any key to continue...



This is similar to the time above

Check if (3 = mystring. length)


10000 cycles: 0.20001 Ms
100000 cycles: 0.90005 Ms
1000000 cycles: 8.50048 Ms
10000000 cycles: 85.7049 Ms
100000000 cycles: 841.64814 Ms
Press any key to continue...



The conjecture is completely correct. When comparing strings, we should first compare whether the references are the same, then compare the length, and then compare them one by one. In this way, we can explain the above comparative test results and prove the resident mechanism.

Conclusion: In mystring! If (0 = mystring. length)
The speed is the fastest. When mystring = "", if (0 = mystring. Length) is the fastest.





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.