值得在 Web 应用程序中使用 StringBuilder 吗?

发布于 2024-07-12 00:24:35 字数 164 浏览 6 评论 0原文

在网络应用程序中,我正在拆分字符串并分配给链接名称或字符串集合。 对 Web 应用程序使用 stringbuilder 是否有显着的性能优势?

编辑:2 个功能:将链接拆分为 5-10 个字符串。 然后重新打包成另一个字符串。 此外,每次单击链接时,我都会一次向链接附加一个字符串。

In web app I am splitting strings and assigning to link names or to collections of strings. Is there a significant performance benefit to using stringbuilder for a web application?

EDIT: 2 functions: splitting up a link into 5-10 strings. THen repackaging into another string. Also I append one string at a time to a link everytime the link is clicked.

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

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

发布评论

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

评论(6

半枫 2024-07-19 00:24:35

您将连接多少个字符串? 您是否确定会有多少条记录,或者这取决于数据库中有多少条记录等?

有关更多详细信息和指南,请参阅我关于此主题的文章 - 但基本上, Web 应用程序与使用 StringBuilder 相比,字符串连接的成本没有区别。

编辑:恐怕从问题中仍然不完全清楚你在做什么。 如果您有一组固定的字符串要连接,并且可以一次完成所有操作,那么使用连接来完成它会更快并且可能更具可读性。 例如:

string faster = first + " " + second + " " + third + "; " + fourth;

string slower = new StringBuilder().Append(first)
                                   .Append(" ")
                                   .Append(second)
                                   .Append(" ")
                                   .Append(third)
                                   .Append("; ")
                                   .Append(fourth)
                                   .ToString();

另一种选择当然是使用格式字符串。 这很可能是最慢的,但最具可读性:

 string readable = string.Format("{0} {1} {2}; {3}",
                                 first, second, third, fourth);

问题中提到“每次添加链接”的部分建议在这方面使用 StringBuilder - 任何自然导致循环的东西都更有效(对于中到大的数字)使用 StringBuilder。

How many strings will you be concatenating? Do you know for sure how many there will be, or does it depend on how many records are in the database etc?

See my article on this subject for more details and guidelines - but basically, being in a web app makes no difference to how expensive string concatenation is vs using a StringBuilder.

EDIT: I'm afraid it's still not entirely clear from the question exactly what you're doing. If you've got a fixed set of strings to concatenate, and you can do it all in one go, then it's faster and probably more readable to do it using concatenation. For instance:

string faster = first + " " + second + " " + third + "; " + fourth;

string slower = new StringBuilder().Append(first)
                                   .Append(" ")
                                   .Append(second)
                                   .Append(" ")
                                   .Append(third)
                                   .Append("; ")
                                   .Append(fourth)
                                   .ToString();

Another alternative is to use a format string of course. This may well be the slowest, but most readable:

 string readable = string.Format("{0} {1} {2}; {3}",
                                 first, second, third, fourth);

The part of your question mentioning "adding a link each time" suggests using a StringBuilder for that aspect though - anything which naturally leads to a loop is more efficient (for moderate to large numbers) using StringBuilder.

翻身的咸鱼 2024-07-19 00:24:35

You should take a look at this excellent article by Jon Skeet about concatenating strings.

若无相欠,怎会相见 2024-07-19 00:24:35

是的,连接常规字符串的成本很高(实际上是将字符串附加到另一个字符串的末尾)。 每次更改字符串时,.net 都会删除旧字符串并使用新值创建一个新字符串。 它是一个不可变的对象。

编辑:

Stringbuilder 应谨慎使用,并像任何其他方法一样进行评估。 有时将两个字符串连接在一起会更有效,并且应该根据具体情况进行评估。

Atwood 有一篇与此相关的有趣文章。

Yes, concatenating regular strings is expensive (really appending on string on to the end of another). Each time a string is changed, .net drops the old string and creates a new one with the new values. It is an immutable object.

EDIT:

Stringbuilder should be used with caution, and evaluated like any other approach. Sometimes connactenting two strings together will be more efficient, and should be evaluated on a case by case basis.

Atwood has an interesting article related to this.

〃安静 2024-07-19 00:24:35

为什么 Web 应用程序或 winforms 应用程序的性能会有所不同?

由于内存和对象分配,使用 stringbuilder 是一个很好的实践,无论您为何构建代码,规则都适用。

Why would the performance be any different in a web application or a winforms application?

Using stringbuilder is a matter of good practice because of memory and object allocation, the rules apply no matter why you are building the code.

清晰传感 2024-07-19 00:24:35

如果您要在循环中进行大量迭代,那么最好使用 stringbuilder。 否则,字符串连接是最好的选择。

If you're making the string in a loop with a high number of iterations, then it's a good idea to use stringbuilder. Otherwise, string concatenation is your best bet.

我是有多爱你 2024-07-19 00:24:35

首先,您还在写这个应用程序吗? 如果是,则停止性能调整!
其次,正确性优先于速度。 从长远来看,可读性更为重要,原因显而易见。
第三,我们不知道您正在编写的确切情况和代码。 我们无法真正建议您这种微优化对于代码的性能是否重要。 测量差异。 我强烈推荐 Red Gate 的 Ants Profiler。

FIRSTLY, Are you still writing this application? If yes then STOP Performance tuning!
SECONDLY, Prioritise Correctness over Speed. Readability is way more important in the long run for obvious reasons.
THIRDLY, WE don't know the exact situation and code you are writing. We can't really advise you if this micro optimisation is important to the performance of your code or not. MEASURE the difference. I highly recommend Red Gate's Ants Profiler.

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