Home  >  Article  >  Backend Development  >  C# string operations--reduce garbage collection pressure

C# string operations--reduce garbage collection pressure

黄舟
黄舟Original
2017-02-13 11:59:031585browse

C# Performance optimization details

1. Use string.Empty to assign an initial value to an empty string variable


  • String.Empty is a reference, and "" is the specific implementation

    string filter=“”;//不建议
     
    string filter=string.Empty; //建议

2. Use str.Length == 0 to compare empty strings


  • ##The fastest method: if (str.Length == 0)

  • ##Secondly: if (str == String.Empty) or if (str == "")



##3. Avoid unnecessary string ToUpper and ToLower operations


Methods such as ToUpper and ToLower will regenerate string pairs

  • String.Compare can ignore the case of strings
  • //不推荐的写法
    if(s1.ToUpper()==s2.ToUpper()) …;
    //推荐的写法
    if(String.Compare( s1, s2, true ) == 0) …;
  • 4. Use StringBuilder skillfully to perform string splicing operations


    If you want to construct For a longer string, especially when it is spliced ​​more than 10 times (experience value), StringBuilder should be used for string splicing operations.
  • [object Object]

  • 5. When creating a StringBuilder, you should specify the initial size


    The default initial size is 16. Once exceeded, Resize is required and GC pressure increases. It is recommended to give it an initial size based on empirical values.
  • StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10; i++)
    {
       sb.Append(i);
    }
    string s = sb.ToString();
    //建议修改为
    StringBuilder sb = new StringBuilder(256);
    for (int i = 0; i < 10; i++)
    {
       sb.Append(i);
    }
    string s = sb.ToString();


6. Avoid abusing StringBuilder


    String concatenation operations similar to str1+str2+str3+str4 will be compiled into String.Concat(str1, str2, str3, str4), which is more efficient than StringBuilder. String.Concat will determine the string length at one time, and StringBuilder needs to do Resize, which is suitable for generating string objects multiple times.


7. Initialize StringBuilder by directly setting .Length=0


    #According to the experimental results, when using the same StringBuilder object multiple times, initialization is fastest by directly setting .Length=0.
  • StringBuiler sb = new StringBuilder(256);
      ......
    sb.Remove(0, sb.Length); //不建议
    sb.Length = 0; //建议


8. Do not use .Length=0 to release the memory occupied by StringBuilder

static void test()
{
    StringBuilder sb = new StringBuilder(256);
    for (int i = 0; i < 100; i++)
    {
        sb.Append(i);
    }
    string t = sb.ToString();
    ……//其他不使用变量sb的代码段
    sb.Length = 0; //去掉该句手工清空sb代码,会更早释放内存
}


9. To be continued


The above is the content of C# string operation--reducing the pressure of garbage collection. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn