C # concatenation faster

I did a visual analysis of the studio in my code, and I found that a lot of time was spent concatenating strings. Is there a faster way to concatenate?

string[] infoseperated = info.Split(' '); for (int iii = totalremove; iii < infoseperated.Length; iii++) { newinfo += infoseperated[iii] + " "; } 
+4
source share
9 answers

use string.Join instead:

 newinfo = string.Join(" ", infoseperated.Skip(totalremove)); 

Your current approach generates a new line for each concatenation (since the lines are immutable and you need to assign a new line), which is quite expensive.

For each concatenation, all characters of an existing line must be copied to a new line, so the cost of this operation increases with the number of characters in a line - this is Schlemiel the Painter Algorithm

string.Join uses StringBuilder inside, which avoids this.

+12
source

you should take a look at the StringBuilder class . It is designed for such things.

+4
source

Each time you combine with the + operator, you create a new object. Use the StringBuilder class StringBuilder .

+1
source

Use System.Text.StringBuilder. It has better performance than string concatenation.

0
source

string.Join , string.Append and string.Format("") are probably more efficient than adding strings, however this does not mean that you will definitely improve your speed as much as you want. You should try to look at the big picture and really determine why you concatenate strings so much, if you can minimize this action, then this may be better than just using the most efficient concatenation method.

0
source

Try using StringBuilder :

  StringBuilder sb = new StringBuilder(); int totalremove = 0; string[] infoseperated = info.Split(' '); for (int iii = totalremove; iii < infoseperated.Length; iii++) { sb.Append(infoseperated[iii]); sb.Append(" "); } newinfo = sb.ToString(); 
0
source

Other answers are true to suggest string.Join , but they suggest incorrect overloading:

 newInfo = string.Join(" ", infoseparated, totalremove, infoseparated.Length - totalremove); 
0
source

Like many other answers, using StringBuilder directly or indirectly with string.Join will be much better. However, in your Split and Join example, I think we can do even better.

 // first, find the n-th (totalremove-th) separating space. int index = 0; for(int i = 0; i < totalremove; i++) { index = infoseperated.IndexOf(' ', index + 1); if(index == -1) return ""; // eg, asked to remove 5 values, but only 3 in the string. } // return everything after that point. return infoseperated.Substring(index + 1); 

As long as the infoseperated object has no double spaces or whitespace at the beginning or something like that, it will be more efficient than splitting and reassembling the string.

0
source

If performance is the biggest problem, the fastest method would probably not be line splitting or memory allocation overhead, other than getting a substring;

 string newinfo; while (totalremove-- > 0 && (index = info.IndexOf(' ', index)) >= 0) index+=1; if (index >= 0) newinfo = info.Substring(index); else newinfo = ""; 
0
source

Source: https://habr.com/ru/post/1403144/


All Articles