Hey guys, im creating a chat system, which stores everything in a list per chat message sent, and then slaps them into a chat log, and im using a + to represent new chat line and the next chat message, but it feels slow when the chat is too big, even if its just for a frame, it might ruin the gameplay.
My question is, is String.Format faster than String + String, and why?
Both String.Format and string concatenation are going to create new strings.
Here’s the deal, a string is immutable, it can not be changed. When you concat, you create a brand new string. Thing is, a string is just a char array underneath.
A very large string takes up a very large chunk of memory. If you have a string that is 8000 chars long, and you want to append 1 letter to it. A new 8001 long char array is created and this last character is put on the end. Resulting in 16001 chars in memory being used up.
.Net has a mutable string class as well. Its interface is very different. But it allows for a much more efficient use of memory. It’s called StringBuilder.
Whoa…dont be adding strings just keep a list and add to it.
List<string> chatLog = new List<string>();
[RPC]
void Chat(string message)
this.chatLog.Add(message);
}
foreach(string message in this.chatLog) {
// do something
}
OR create a UI list so you don’t have to
Polymorphik, im using a list just to store each chat message that is sent(because i need it afterwards to remove the top line and re-add the chat), but for the actual chat i always use a single string, that keeps adding a new line + the new message, and this is because i dont know any other way on doing a chat for the chat logs.
Use StringBuilder.
Note the AppendLine method:
How is it supposed to be in C# ?
That link is to MSDN, the link in my first post was to the full SringBuilder article on MSDN. It’s in C#. Scroll to the bottom of the full StringBuilder article, and there’s a ton of examples on how to use it.
Many thanks, ill check it out!
Hm, im not sure this builder is useful for me, do you want me to show you the code?
You’re saying you have a list of strings, and that you need to append them all together. You’re using the + (concat) operation to do that.
I explained that EVERY time you use +, a duplicate of the string appears in memory. This is why when that list gets super long, it slows down a lot.
If you use StringBuilder, 1 mutable string that resizes as needed is created. This makes more efficient use of memory. Reducing the slow down, because you’re not duplicating the string every time you append a string to it.
Is this making sense?
Not only a list, before that ,every chat message that is sent , i use string + newAddition on a UI.text, which is the more active culprit :
IEnumerator ApplyGlobalChatText(string str){
chatField.text += “\n” + str;
ect…
Also everything makes sense, just that i might be missing something mostly because i was working on two projects at the same time today, the whole day and im tired
I have just tried the builder on the list, and it seems to be working! But i still think that the actual chat field cannot be done this way.
here is the list part:
if(cPool.Count > 30){
cPool.RemoveAt(0);
// chatField.text = “”;
for(int i = 0; i < cPool.Count; i++){
sb.Append(“\n” + cPool*);*
// chatField.text += “\n” + cPool*;*
}
chatField.text = sb.ToString();
sb = new StringBuilder();
}
Use code tags.
And yes it can be done. You’re just going to have to structure it correctly. I do not know what your implementation is exactly, but from the sounds of it, you need to stop treating the ui text as the storage mechanism, and rather as a display mechanism and only a display mechanism. What I mean is that instead of concatinating to the existing text in the ui, you set the text to the feed as is stored in your StringBuilder.
Oh, and don’t create a new string builder every time. Have a stringbuilder, and clear it if you need to blank it out.
Alright.
And yeah i dont make a new builder every time, only when i need to clear a line of text or hit the line limits ![]()
Also
bool gotIt = true;