String vs. StringBuilder

后端 未结 24 1253
眼角桃花
眼角桃花 2020-11-22 06:22

I understand the difference between String and StringBuilder (StringBuilder being mutable) but is there a large performance difference

相关标签:
24条回答
  • 2020-11-22 06:56

    Yes, StringBuilder gives better performance while performing repeated operation over a string. It is because all the changes are made to a single instance so it can save a lot of time instead of creating a new instance like String.

    String Vs Stringbuilder

    • String

      1. under System namespace
      2. immutable (read-only) instance
      3. performance degrades when continuous change of value occures
      4. thread safe
    • StringBuilder (mutable string)

      1. under System.Text namespace
      2. mutable instance
      3. shows better performance since new changes are made to existing instance

    Strongly recommend dotnet mob article : String Vs StringBuilder in C#.

    Related Stack Overflow question: Mutability of string when string doesn't change in C#?.

    0 讨论(0)
  • 2020-11-22 06:56

    String concatenation will cost you more. In Java, You can use either StringBuffer or StringBuilder based on your need. If you want a synchronized, and thread safe implementation, go for StringBuffer. This will be faster than the String concatenation.

    If you do not need synchronized or Thread safe implementation, go for StringBuilder. This will be faster than String concatenation and also faster than StringBuffer as their is no synchorization overhead.

    0 讨论(0)
  • 2020-11-22 06:56

    StringBuilder will perform better, from a memory stand point. As for processing, the difference in time of execution may be negligible.

    0 讨论(0)
  • 2020-11-22 06:58

    StringBuilder is probably preferable. The reason is that it allocates more space than currently needed (you set the number of characters) to leave room for future appends. Then those future appends that fit in the current buffer don't require any memory allocation or garbage collection, which can be expensive. In general, I use StringBuilder for complex string concatentation or multiple formatting, then convert to a normal String when the data is complete, and I want an immutable object again.

    0 讨论(0)
  • 2020-11-22 06:59

    Using strings for concatenation can lead to a runtime complexity on the order of O(n^2).

    If you use a StringBuilder, there is a lot less copying of memory that has to be done. With the StringBuilder(int capacity) you can increase performance if you can estimate how large the final String is going to be. Even if you're not precise, you'll probably only have to grow the capacity of StringBuilder a couple of times which can help performance also.

    0 讨论(0)
  • 2020-11-22 07:00

    StringBuilder is significantly more efficient but you will not see that performance unless you are doing a large amount of string modification.

    Below is a quick chunk of code to give an example of the performance. As you can see you really only start to see a major performance increase when you get into large iterations.

    As you can see the 200,000 iterations took 22 seconds while the 1 million iterations using the StringBuilder was almost instant.

    string s = string.Empty;
    StringBuilder sb = new StringBuilder();
    
    Console.WriteLine("Beginning String + at " + DateTime.Now.ToString());
    
    for (int i = 0; i <= 50000; i++)
    {
        s = s + 'A';
    }
    
    Console.WriteLine("Finished String + at " + DateTime.Now.ToString());
    Console.WriteLine();
    
    Console.WriteLine("Beginning String + at " + DateTime.Now.ToString());
    
    for (int i = 0; i <= 200000; i++)
    {
        s = s + 'A';
    }
    
    Console.WriteLine("Finished String + at " + DateTime.Now.ToString());
    Console.WriteLine();
    Console.WriteLine("Beginning Sb append at " + DateTime.Now.ToString());
    
    for (int i = 0; i <= 1000000; i++)
    {
        sb.Append("A");
    }
    Console.WriteLine("Finished Sb append at " + DateTime.Now.ToString());
    
    Console.ReadLine();
    

    Result of the above code:

    Beginning String + at 28/01/2013 16:55:40.

    Finished String + at 28/01/2013 16:55:40.

    Beginning String + at 28/01/2013 16:55:40.

    Finished String + at 28/01/2013 16:56:02.

    Beginning Sb append at 28/01/2013 16:56:02.

    Finished Sb append at 28/01/2013 16:56:02.

    0 讨论(0)
提交回复
热议问题