开发者

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
    {
      开发者_如何学Go   _masterText.Append(text);  
    }

Thanks


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.


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.


Every 200ms isnt a very taxing poll, regardless stringbuilder is always better.


If you're up for a read, I think the discussion at http://dotnetperls.com/stringbuilder-1 is really useful. Check out the links to real metrics on speed and memory usage.

Also, check out Joel Spolsky's discussion of "Shlemeil the Painter's Algorithm". Although he's talking about C's strcat function, the principle applies to C#'s plus operator on strings. Besides, it's good for a laugh.

In general, I recommend StringBuilder if you are doing the append operation rapidly or with many large strings.


StringBuilder is your buddy!


It depends upon the scenario as well. No doubt it is faster to add with the generic list as compare to the stringbuilder object. But during the time of data retrieval from the generic list, it will be much slower as compare to the stringbuilder object.

The stringbuilder will return quickly using _masterText.ToString() but with the generic list, you might have to pull the value using the iteration. And that will be expensive process such as :-

  for (int x = 0; x < 100; x++)
    {
      Label3.Text += gen_list[x];
    }

Or you can try with

Label3.Text = string.Join("", gen_list.ToArray());

then the retrieval operation will be slower and expensive, and you can notice the CPU spike easily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜