I. Parsing string class strings Append
When we stitch a string, string: Is the object is not the original type. As an immutable object, you cannot modify its value once it is created. Modifying a String object that already exists is to recreate a new object and then save the new value in. String is a sealed class, which cannot be inherited.
String is the alias of the string class, the reference type, and the type is read-only, non-modifiable, and when you modify the contents of the string, you actually create another new string and point the reference to it, you can refer to the constructor description of the string type, and you know where the error is. The first one simply creates an array of type string, does not initialize it, and if you initialize it using the second method for any member of the array, it will be an error.
str + = "a"; Equivalent to: str = new stringbuffer(str). Append ("a"). ToString(); Although the compiler optimizes the string plus sign, it is appended with the StringBuffer append method. It is then converted into string strings by the ToString method. It's different from the pure Append method.:
One is to create a StringBuilder object each time ;
The second is to call the ToString method to convert it to a string after each execution .
Therefore, appending characters to a string type can take a significant amount of time, since each append completes with the return of a new string, and each operation creates a new, string Object , which is why the direct use slows down.
String Append of StringBuffer
StringBuffer: is a mutable object that, when modified, does not re-establish the object like a string, it can only be established by a constructor, StringBuffer sb = new StringBuffer (); it cannot be assigned directly to it. SB = "I Hava a Dream";//error, after the object is created, the memory space is allocated in memory, and a null is initially saved. When you assign a value to a stringbuffer, you can pass its Append method. Sb.append ("of course");
StringBuilder
String immutable, long-term splicing string performance is low
Stringbuilder
Append (string);
Appendline (string);
AppendFormat ("{0}{1}{2}{3}", 1, "+", "AB", true);
ToString ();
So it has to be assigned by appending the method. Its internal implementation code is as follows:
1 Publicabstractstringbuilder Append (String str) {2 //if it is a null value, the null is treated as a string3 if(str = =NULL) str ="NULL"; 4 5 intLen =str.length ();6 //string has a length of 0, it returns itself7 if(len = =0)return This; 8 9 intNewcount = Count +Len;Ten //whether the appended string group length exceeds the current value One if(Newcount >value.length) AExpandcapacity (Newcount);//length, and count group copy - //copy string to destination array -Str.getchars (0, Len, value, count); theCount =Newcount; - - return This; -}
When appending in StringBuffer, you do not need to create a new object, open a new space, it is always an object, and a string object is an immutable object, and each operation Sting re-establishes a new object to hold the new value.
So the original object is useless, it will be garbage collection. This is also to affect performance.
Second, scene simulation
We use the stopwatch class to simulate the two cases separately, and in C # there is a stopwatch class: stopwatch, which can be used to test the code run time in a convenient way.
1Stopwatch sp=NewStopwatch ();2 Sp.start ();3 strings="";4 for(intI=0;i<100000; i++)5 {6s+=i.tostring ();7 }8 sp. Stop ();9 Console.WriteLine (sp. Elapsed);TenConsole.readkey ();
The results show that the time spent more than 30 seconds
And for StringBuilder:
1Stopwatch sp=NewStopwatch ();2 Sp.start ();3StringBuilder sb=NewStringBuilder ();4 for(intI=0;i<100000; i++)5 {6s+=sb. Append (i.ToString ());7 }8 sp. Stop ();9 Console.WriteLine (sp. Elapsed);TenConsole.readkey ();
The results show that the time spent 0. A few seconds, a difference of two orders of magnitude
Third,Conclusion
When our system performance is not critical, the string type of simple answer to the comparison of ground gas, in line with people's usual habit of writing code, and when systems are critical, we want to carry out a large number of cycles, we should use StringBuilder, improve our code optimization.
String performance analysis of three classes of libraries (one)