对一个字符串使用多个字符串生成器

发布于 2024-11-27 01:11:46 字数 1934 浏览 3 评论 0原文

因此,我主要是一名 C# 和 Java 开发人员,但我认为这个问题可能与使用 StringBuilder 或其某些衍生产品的任何编程语言有关。

好吧,或多或少的常识是,即使连接少量的字符串也可能成为主要的性能杀手(尽管这一点也是有争议的)。我的问题是,是否有人了解在字符串生成器中使用字符串生成器的性能影响。为了澄清我的意思,让我添加一些有助于说明我的观点的虚拟代码。

我还意识到,调用 StringBuilder 的多个实例自然会对性能造成影响,但我不相信我会调用它足以导致任何真正的性能问题。 (这个假设也可能是错误的,任何对此的看法也会有帮助)

public string StringToGet()
{
     StringBuilder sb = new StringBuilder("Some Text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append(MethodThatCreatesAnotherString());
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append(MethodThatCreatesAnotherString());
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append(MethodThatCreatesAnotherString());
     //etc   

     return sb.toString();

}

private string MethodThatCreatesAnotherString()
{
     StringBuilder sb = new StringBuilder("Other text");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");

     return sb.ToString();
}

逻辑告诉我这不应该是一个问题,但这种方法的某些东西对我来说似乎并不正确。任何人都可以阐明以下问题吗?

  1. 与不使用其他方法和使用单个 StringBuilder 相比,这是否会造成明显更大的性能损失,
  2. 这是一种合理可接受的做法。

任何有关这方面的现场将不胜感激。

So I am primarily a C# and Java developer but I suppose this question could pertain to any programming languages that uses StringBuilder or some derivative thereof.

Okay so it is more or less common knowledge that concatenate even a small number of strings can be a major performance killer (although even that is debatable). My question is does anyone have knowledge of the performance effects of using a string builder within a string builder. To clarify what I mean by that let me include some dummy code that should help illustrate my point.

Also I realize that there is naturally a performance hit by calling multiple instances of StringBuilder but I do not believe that I would be calling it enough to cause any real performance issues. (This assumption may also be wrong any opinion on that would be helpful as well)

public string StringToGet()
{
     StringBuilder sb = new StringBuilder("Some Text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append(MethodThatCreatesAnotherString());
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append(MethodThatCreatesAnotherString());
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append(MethodThatCreatesAnotherString());
     //etc   

     return sb.toString();

}

private string MethodThatCreatesAnotherString()
{
     StringBuilder sb = new StringBuilder("Other text");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");
     sb.append("more text to add");

     return sb.ToString();
}

Logic tells me that this shouldn't be a problem, but something about this approach just does not seem to look right to me. Can anyone shed some light on the following questions

  1. Does this create a significantly larger performance hit than just not using additional methods and using a single StringBuilder
  2. Is the a reasonably acceptable practice.

Any onsite on this would be greatly appreciated.

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

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

发布评论

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

评论(3

浅浅淡淡 2024-12-04 01:11:46

通常,如果直接字符串连接的数量超过 5 个,最好使用 StringBuilder。如果您可以控制依次构建字符串的方法,为什么不考虑将 StringBuilder 传递到该方法中呢?

public void MethodThatCreatesAnotherString(StringBuilder sb)
{
   // code that appends to StringBuilder
}

因此,当需要调用该函数时,您可以传入当前的 StringBuilder,并且不会损失性能,但这是以潜在的混淆为代价的。

也就是说,除非您要进行数百或数千个这样的操作,否则我不会担心这里的过早优化。您的主要性能损失是必须执行两次(即在方法内附加字符串,然后附加完整的字符串),但您仍然不会将性能损失达到使用的程度+ 连接。

编辑:如何使用它的澄清示例:

string SomeMethodCreatingAString()
{
   StringBuilder sb = new StringBuilder();
   sb.append("more text to add");
   sb.append("more text to add");
   sb.append("more text to add");
   sb.append("more text to add");
   sb.append("more text to add");
   MethodThatCreatesAnotherString(sb);
   // more text
   return sb.ToString();
}

因此,您永远不会从第二个方法附加该字符串,只需在该方法中使用相同的StringBuilder即可。希望这是有道理的。

Typically, if the number of straight-up string concatenations are more than 5, it's better to use StringBuilder. If you have control over the methods that in turn build a string, why not consider passing your StringBuilder into the method?

public void MethodThatCreatesAnotherString(StringBuilder sb)
{
   // code that appends to StringBuilder
}

So, when it's time to call that function, you pass in your current StringBuilder, and you don't lose a performance hit, but it's at the expense of potential obfuscation.

That said, unless you're getting into the hundreds or thousands of these operations, I wouldn't worry about premature optimization here. Your main performance hit is having to do it twice (i.e. append the strings inside the method, and then appending the complete string), but you still won't be taking the performance hit to the extent of using + concatenation.

Edit: A clarifying example of how to use it:

string SomeMethodCreatingAString()
{
   StringBuilder sb = new StringBuilder();
   sb.append("more text to add");
   sb.append("more text to add");
   sb.append("more text to add");
   sb.append("more text to add");
   sb.append("more text to add");
   MethodThatCreatesAnotherString(sb);
   // more text
   return sb.ToString();
}

So, you don't ever append that string from the second method, you simply use the same StringBuilder within that method. Hopefully that makes sense.

满天都是小星星 2024-12-04 01:11:46

我不确定对性能的影响,但更改 MethodThatCreatesAnotherString 方法的签名以接受 StringBuilder 将消除对 StringBuilder 的多个实例的需要。

I am not sure about the performance implications but changing the signature of your MethodThatCreatesAnotherString method to accept a StringBuilder would remove the need for multiple instances of StringBuilder.

杀お生予夺 2024-12-04 01:11:46

作为 http://www. codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html 显示不同的字符串连接方式之间的差异确实很小直到达到非常大量的串联。

因此,除非您已经知道此代码存在性能问题,否则我认为没问题。在您的示例中,您实际上只获得了 3 个额外的字符串构建器初始化和 3 个额外的字符串分配。

从长远来看,您应该尝试做最简单的事情,并且仅在确定需要它们并且测试将其暴露出来时才担心复杂的微观优化。

As http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html shows the difference between different ways of concatenating strings is really pretty minor until you get to really large numbers of concatenations.

Therefore, unless you already know you have a performance problem with this code then I think its fine. Your really only getting 3 extra string builder inits and 3 extra string allocations in your example.

In the long run you should just try to do what is simplest and only worry about complicated micro optimizations if you are sure you need them and testing bares them out.

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