简体   繁体   中英

C# concatenation quicker

I ran visual studio analysis on my code and i found that a large amount of time was spent concatenating strings. Is there a quicker way to concatenate?

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

use string.Join instead:

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

Your current approach generates a new string for every concatenation (since strings are immutable and you have to re-assign a new string), which is quite expensive.

For every concatenation all characters of the existing string have to be copied into the new string, so the cost of this operation grows with the number of characters in a string - it's a Schlemiel the Painter's algorithm

string.Join uses a StringBuilder internally which avoids this.

you should take a look at the StringBuilder class . It is meant for things like this.

Every time you concatenate with the + operator, you're creating a new object. Instead, use the StringBuilder class.

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

string.Join , string.Append and string.Format("") are probably more efficient than adding strings, however that doesn't mean that you will necessarily improve your speed as much as you really want to. You should try to look at the big picture and really determine why you're concatenating strings so much, if you can minimize that action then it might be better than just using the most efficient concatenation method.

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();

其他答案是正确的建议string.Join ,但他们建议错误的重载:

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

As numerous other answers have said, using a StringBuilder either directly or indirectly through string.Join will be much better performance. However, in your example Split & Join, 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 ""; // e.g., asked to remove 5 values, but only 3 in the string.
}

// return everything after that point.
return infoseperated.Substring(index + 1);

As long as infoseperated doesn't have any double-spaces, or a space at the beginning, or anything like that, this will be more efficient than splitting & reassembling the string.

If performance is the greatest concern, the fastest method would probably be not to split the string at all and incur no memory allocation overhead except getting the substring;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM