C# += (plus equals)(加法赋值)当字符串太长时工作速度非常慢?

发布于 2024-08-11 15:41:02 字数 179 浏览 3 评论 0原文

我有一个 for 循环,我所做的就是这个。

forloop ( loop 7000 times)
{
x += 2000_char_long_string;
}

代码在这个 forloop 中持续的时间非常长,可能超过 1 分钟。我该如何解决这个问题?

谢谢。

I have a for loop and what I do is this.

forloop ( loop 7000 times)
{
x += 2000_char_long_string;
}

Code lasts really long time in this forloop, maybe more than 1 minute. How can I solve this problem?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

〆一缕阳光ご 2024-08-18 15:41:02

使用 StringBuilder

StringBuilder sb = new StringBuilder();
for(int i=0; i< 200; i++){
   sb.append(longString);
}
return sb.ToString();

当您在字符串上使用 += 时,您会不断创建新字符串,并不断定位越来越大的内存块。这就是操作如此缓慢的原因。

Use a StringBuilder.

StringBuilder sb = new StringBuilder();
for(int i=0; i< 200; i++){
   sb.append(longString);
}
return sb.ToString();

When you use += on strings, you keep creating new strings, and keep locating larger and larger blocks of memory. That is why the operation is so slow.

花想c 2024-08-18 15:41:02

使用 StringBuilder,如 @Kobi 引用 您仍然可以通过初始化它来提高性能。看来你可以提前确定字符串的最终大小。

StringBuilder sb = new StringBuilder(7000*2000);
for(int i=0; i< 7000; i++){
   sb.append(2000_char_long_string);
}
return sb.ToString();

Using a StringBuilder, as @Kobi referenced you can still increase performance by initializing it. It seems you can determine the final size of the string in advance.

StringBuilder sb = new StringBuilder(7000*2000);
for(int i=0; i< 7000; i++){
   sb.append(2000_char_long_string);
}
return sb.ToString();
謸气贵蔟 2024-08-18 15:41:02

字符串操作是不可变的。每次 stmt x += 2000_char_long_string; 都会创建一个新字符串;被执行。因此,按照 Kobi 的建议,您应该使用 StringBuilder 类。

但是,在您的情况下,您应该在 StringBuilder 构造函数中指定容量。

这是因为,如果不指定,StringBuilder在创建时的默认容量是16。

一旦这个容量用完,它会创建一个新的连续内存位置,将StringBuilder的所有内容复制到新位置,并将实例指向新地点。由于您已经知道最终字符串的大致大小(可能是 7000 * 2000),因此您可以相应地指定容量。

有关更多详细信息,请参阅我对 StringBuilder 和容量? 的回答。

String manipulations are immutable. There will be a new string created everytime the stmt x += 2000_char_long_string; is executed. Hence as suggested by Kobi, you should use a StringBuilder class.

However, in your case, you should specify the capacity in the StringBuilder constructor.

This is because, if not specified, the default capacity of StringBuilder during creation is 16.

Once this capacity is exhausted, it will create a new contiguous memory location, copy all the contents of the StringBuilder to the new location and point the instance to the new location. Since you are already aware of the approximate size the final string would be (maybe 7000 * 2000), you can specify the capacity accordingly.

Please see my answer to StringBuilder and capacity? for more details.

请止步禁区 2024-08-18 15:41:02

您应该使用 StringBuilder

You should use the StringBuilder class

七色彩虹 2024-08-18 15:41:02

C# 字符串对象是不可变的,每次内容更改时都会创建新对象并复制新内容。请改用 StringBuilder,它是为了解决您面临的问题而提供的

C# string object is immutable, every time the contents are changed new object is created and the new contents are copied. Use StringBuilder instead, it's provided to address the issue that you are facing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文