简体   繁体   English

C#连接更快

[英]C# concatenation quicker

I ran visual studio analysis on my code and i found that a large amount of time was spent concatenating strings. 我对我的代码运行了visual studio分析,我发现花了很多时间来连接字符串。 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: 使用string.Join代替:

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 对于每个连接,现有字符串的所有字符都必须复制到新字符串中,因此此操作的成本随着字符串中字符数的增加而增长 - 这是Schlemiel the Painter的算法

string.Join uses a StringBuilder internally which avoids this. string.Join在内部使用StringBuilder ,避免了这种情况。

you should take a look at the StringBuilder class . 你应该看一下StringBuilder类 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. 而是使用StringBuilder类。

Use System.Text.StringBuilder. 使用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. string.Joinstring.Appendstring.Format("")可能比添加字符串更有效,但这并不意味着你必须像你真正想要的那样提高你的速度。 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

        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. 正如许多其他答案所说,使用StringBuilder直接或间接通过string.Join会有更好的性能。 However, in your example Split & Join, I think we can do even better. 但是,在您的示例Split&Join中,我认为我们可以做得更好。

// 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. 只要infoseperated没有任何双重空间,或开头的空间,或类似的东西,这将比分割和重新组装字符串更有效。

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 = "";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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