In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of c# System.String for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Basic concept
The string (strictly speaking, System.String) type is one of the most frequently used types in our daily coding. What is String? ^ ~ ^
String is an immutable collection of consecutive 16-bit Unicode code values derived directly from the System.Object type.
There is also an uncommonly used secure string type, System.Security.SecureString, which is allocated on unmanaged memory to avoid the black hands of GC. It is mainly used in scenarios with high security. For more information, please see msdn. We will not discuss it here. = > msdn to view details
Characteristics
Because the String type derives directly from Object, it is a reference type, which means that an instance of the String object always exists on the heap.
String is immutable, which means that once initialized, its value will never change.
String types are closed, in other words, none of your types can inherit String.
The keyword string that defines a string instance is just a mapping of the System.String type.
Matters needing attention
With regard to carriage returns and newline characters in strings, we generally like to hard-code'\ r\ ncharacters directly, but this is not recommended. Once the program is migrated to another platform, an error will occur. Instead, it is recommended to use the NewLine property of the System.Environment class to generate carriage returns and newline characters, which can be used across platforms.
The concatenation of constant strings and non-constant strings behave differently in CLR. Please check the performance section for details.
Adding the @ symbol before the string changes the compiler's behavior. If the @ symbol is added, the compiler will display the escape characters in String as normal characters. That is, what I define is the content, mainly used in the use of file paths or directory strings. The output of the following two String contents will be exactly the same.
Static void Main (string [] args) {string a = "c:\\ temp\\ 1"; string b = @ "c:\ temp\ 1"; Console.WriteLine (a); Console.WriteLine (b); Console.Read ();}
Performance
The compiler of C # directly supports the String type and stores the defined constant strings directly into the module's metadata at compile time. It is then loaded directly at run time. This also shows that constants of type String are given special treatment at run time.
Because of the invariance of the string, it means that there is no thread safety problem for multiple threads to operate on the string at the same time. This is useful in some shared configuration designs.
If the program often deals with strings with high specific gravity, this will have a performance impact, because comparing strings takes several steps. For this reason, CLR introduces a technique of string reuse, which is called string retention. The principle is that CLR creates an internal hash table during initialization, key is a string, and value is a reference to the retained string on the managed heap.
The String type provides two static methods to manipulate the hash table:
String.Intern
String.IsInterned
For more information, please see msdn (https://msdn.microsoft.com/zh-cn/library/system.string.isinterned(v=vs.110).aspx)
However, the c # compiler does not turn on string retention by default, because if the program retains a large number of strings, the overall performance of the application may become slower. (Microsoft is also very tangled, programmer TMD is more entangled)
If there are many constant strings of exactly the same value in our program, the compiler of c # will merge these strings into one and write them into the module's metadata during compilation, and then modify all the code that references the string. This is also a string reuse technique, the scientific name 'string pool'. What does that mean? This means that all constant strings with the same value actually refer to instances of the same memory address, which can significantly improve performance and save a lot of memory when there are a lot of the same values.
String S1 = "hello dish"; string S2 = "hello dish"; unsafe {fixed (char* p = S1) {Console.WriteLine ("string address = 0x {0string x}", (int) p);} fixed (char* p = S2) {Console.WriteLine ("string address = 0x {0char* x}", (int) p);}}
Output result:
String address = 0x80002d84
String address = 0x80002d84
The value of the visible instance is assigned only once, but it is important to note that strings are only used for strings that can determine the value at compile time, that is, constant strings. If my program is modified to:
Args = new string [] {"dfasfdsa"}; string S1 = "hello cuisine" + args [0]; string S2 = "hello cuisine" + args [0]; unsafe {fixed (char* p = S1) {Console.WriteLine ("string address = 0x {string x}", (int) p);} fixed (char* p = S2) {Console.WriteLine ("string address = 0x {0string x}", (int) p);}
Running result:
String address = 0x2e3c
String address = 0x2e7c
Normally, coding cannot avoid string concatenation. If you use'+'in a scenario where strings are stitched frequently, it will have a great impact on the overall performance of the program and GC. For this reason, c # introduced the StringBuilder type to optimize string stitching. StringBuilder is more like a mutable string type than the immutability of the String type. Its underlying data structure is an array of Char. There are also attributes such as capacity (default is 16), maximum capacity (default is int.MaxValue), and so on. The advantage of StringBuilder is that when the total number of characters does not exceed the "capacity", the underlying array will not be reallocated, which is the biggest contrast to the reallocation of String every time. If the total number of characters exceeds the 'capacity', StringBuilder will automatically multiply the capacity property, using a new array to hold the original value, and the original array will be reclaimed by GC. It can be seen that frequent dynamic expansion of StringBuilder will also damage performance, but the impact may be much smaller than that of String. Setting the initial capacity of StringBuilder reasonably is of great help to the program. The tests are as follows:
Int count = 1000000 position Stopwatch sw = new Stopwatch (); sw.Start (); string s = ""; for (int I = 0; I
< count; i++) { s += i.ToString(); }sw.Stop();Console.WriteLine(sw.ElapsedMilliseconds); 运行结果: 14221 查看GC的情况Gc executes so frequently. The performance is imaginable. Then take a look at StringBuilder.
Int count = 1000000 count; Stopwatch sw = new Stopwatch (); sw.Start (); StringBuilder sb = new StringBuilder (); / / I have heard that programmers all name StringBuilderfor (sb.Append (i.ToString ());} sw.Stop (); Console.WriteLine (sw.ElapsedMilliseconds)).
Running result:
twelve
GC situation:
There is almost no GC (which may not have reached the tipping point to trigger GC), and if I initialize the StringBuilder capacity properly, the result gap in the production environment will be even greater. Hehe ^ ~ ^
Other
About string retention and string pooling
When an assembly is loaded, CLR retains all text constant strings described in the assembly metadata by default. Because of the possible performance degradation caused by additional hash table lookups, you can now disable this feature.
In coding, we usually compare whether two strings are equal, so what is the process?
First of all, determine whether the number of characters is equal.
CLR compares characters one by one to finally determine whether they are equal.
This scenario is suitable for string retention. Because you no longer need to go through the above two steps, you can compare and determine it by directly getting the hash table to value.
On string concatenation performance
Based on all the above knowledge, is it true that the performance of StringBuilder concatenation strings is always better than the symbol'+'? The answer is no.
Static void Main (string [] args) {int count = 1000000000; Stopwatch sw = new Stopwatch (); sw.Start (); string str1 = "str1", str2 = "str2", str3 = "str3"; for (int I = 0; I < count; iTunes +) {string s = str1 + str2 + str3;} sw.Stop (); Console.WriteLine ($@ "+ usage: {sw.ElapsedMilliseconds}"); sw.Reset (); sw.Start () For (int I = 0; I < count; iTunes +) {StringBuilder sb = new StringBuilder (); / / I hear programmers name StringBuilder sb.Append (str1) .append (str2) .append (str3);} sw.Stop (); Console.WriteLine ($@ "StringBuilder.Append usage: {sw.ElapsedMilliseconds}"); Console.Read ();}
Running result:
+ time: 553
StringBuilder.Append time: 975
The symbol'+ 'eventually calls the String.Concat method. When concatenating several strings at the same time, memory is not allocated for each connection, but several characters are used as parameters of the String.Concat method, allocating memory only once. Therefore, in the case of a small number of concatenated strings, the performance of String.Concat is slightly higher than that of StringBuilder.Append. The string.Format method finally calls StringBuilder. We will not discuss it here. Please refer to other documents for yourself.
So everything is not absolute! Everything has its own scene, and we all need to explore it ourselves. (programmers are too tired)
This is the end of this article on "sample Analysis of System.String of c #". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.