Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringBuilder or +=

I receive around 5 messages per second. Each of them has a string, which I concatenate to a master string that contains all the received messages

    string _masterText = "";
    public void AddNewMessage(string text)  // this is going to be call at least 5 times/second
    {
        _masterText += text;    
    }

Is this the appropiated way to do it? or should I use instead StringBuilder, like:

    StringBuilder _masterText = new StringBuilder();
    public void AddNewMessage(string text) // this is going to be call at least 5 times/second
    {
         _masterText.Append(text);  
    }

Thanks

like image 677
pedroruiz Avatar asked Jul 29 '10 02:07

pedroruiz


People also ask

What is the difference between StringBuilder and string?

StringBuilder is mutable. StringBuilder performs faster than string when appending multiple string values. Use StringBuilder when you need to append more than three or four strings. Use the Append () method to add or append strings to the StringBuilder object.

How to create a StringBuilder object in Java?

You can create an object of the StringBuilder class using the new keyword and passing an initial string. The following example demonstrates creating StringBuilder objects. using System.Text; // include at the top StringBuilder sb = new StringBuilder(); //string will be appended later //or StringBuilder sb = new StringBuilder("Hello World!");

Should I replace string with StringBuilder in Java?

Although the StringBuilder class generally offers better performance than the String class, you should not automatically replace String with StringBuilder whenever you want to manipulate strings.

How do I append a string to a StringBuilder?

Hello C#. Use the AppendFormat () method to format an input string into the specified format and append it. Use the Insert () method inserts a string at the specified index in the StringBuilder object. StringBuilder sb = new StringBuilder("Hello World!"); sb.Insert (5," C#"); Console.WriteLine (sb); //output: Hello C# World!


2 Answers

StringBuilder is generally considered the better option, but in this case I'd use neither.

Even with StringBuilder, at that rate the underlying character buffer will soon itself be large enough to get stuck on the Large Object Heap. This will cause problems for the health of an application that needs to stay running for a while.

Instead, I'd use a System.Collections.Generic.List<string> and just call it's .Add() method for each new message. Depending on what you do with these messages you may also find another collection type is more appropriate (perhaps a Queue<string>), but this is the direction you should go. By using a collection, the memory used by each individual string will not count towards the size of the collection object. Instead, each string will only add a few bytes for the reference. This will take a lot longer to run into problems with compacting the Large Object Heap.

If you still have problems after switching to a collection, you can use a stream, and write the strings to disk. This way, you never have more than one string in RAM at a time. Now, the only way you have problems is if individual strings are 85000 bytes or larger.

like image 133
Joel Coehoorn Avatar answered Jan 01 '23 10:01

Joel Coehoorn


Remember that the String class is immutable. It is not possible to change a string. When you are "concatenating" strings, you are really creating a new string, and copying the contents of the original string to it, then adding the contents of your new string.

This starts to use memory very quickly if you're appending large strings.

like image 24
John Saunders Avatar answered Jan 01 '23 09:01

John Saunders