StringBuilder的默认容量

发布于 2024-07-08 05:47:02 字数 72 浏览 6 评论 0原文

StringBuilder 的默认容量是多少?

什么时候应该(或不应该)使用默认值?

What is the default capacity of a StringBuilder?

And when should (or shouldn't) the default be used?

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

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

发布评论

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

评论(8

苯莒 2024-07-15 05:47:02

StringBuilder的默认容量是16个字符(我使用.NET Reflector来找出)。

The default capacity of StringBuilder is 16 characters (I used .NET Reflector to find out).

情仇皆在手 2024-07-15 05:47:02

默认值为 16,这似乎是 .NET 框架中任何类型的数组或列表的默认容量。
StringBuilder 上需要的重新分配次数越少越好。
同时,也没有必要分配过多的资金。

我通常通过对 StringBuilder 的最终大小进行某种类型的粗略估计来实例化 StringBuilder。
例如,这可能基于稍后将用于构建字符串的某些迭代计数,乘以本次迭代中每个项目所需的大小。

// where 96 is a rough estimate of the size needed for each item
StringBuilder sb = new StringBuilder ( count * 96 );
for ( int i = 0; i < count; i++ )
{
...
}

当 StringBuilder 的大小太小而无法写入下一个字符串时,
StringBuilder 的内部 char 数组被重新分配为其当前大小的两倍。

Default is 16, which seems to be the default capacity of any type of array or list in the .NET framework.
The less number of reallocations you need on your StringBuilder, the better it is.
Meanwhile, it is unnecessary to allocate much more than is needed, too.

I usually instantiate the StringBuilder with some type of rough estimate as to the final size of the StringBuilder.
For instance, this could be based on some iteration count that you will use later on to build the string, times the size needed for each item in this iteration.

// where 96 is a rough estimate of the size needed for each item
StringBuilder sb = new StringBuilder ( count * 96 );
for ( int i = 0; i < count; i++ )
{
...
}

When the size of a StringBuilder is too small for writing the next string,
the internal char array of StringBuilder is reallocated to twice its current size.

机场等船 2024-07-15 05:47:02

今天这个问题与另一个问题重复出现,但我注意到有一部分没有得到解答。 正如人们所说,默认值(假设这意味着“当没有使用足够大的字符串创建时需要 )是 16 ,但是我在这里没有看到任何关于何时应该更改它的信息。

当您可以这样做时,您可以更改它, 如下所示事实上,选择 16 与优化相反,优化是选择值和方法,以便特别适合特定情况或可能情况的子集(而不是“制造事物”)。更快”,尽管我们经常使用这个词)。这里类的设计者必须处理泛化 - 选择值和方法,以便在广泛的情况下提供相当好的性能它们

越小,使用的内存就越少

,处理较大字符串的重新分配就越少。

有几个原因可以解释为什么二进制轮(2 的整数次幂)可能比其他数字提供更好的性能。某些情况下,所以他们选择了其中一种,但除了 4 或 16 或 1024 之间的选择之外,还有一个平衡不同可能值的问题。

使用StringBuilder而不是设计它的人可能会更好地了解他们可能需要什么大小。

如果他们要 Append 5 个 1 位数字以及总长度为 43 个字符的字符串,则 StringBuilder 的总长度将是 48 个字符不管怎样,所以他们应该使用 48 的容量,因为 48 始终是 48 长度的字符串最有效的大小。

如果他们正在做的事情可能有大约 23 到 34 个字符之间的任何长度,那么他们应该使用 34。

如果他们正在做的事情可能永远不会超过 60 个字符,但偶尔会有,他们应该 使用 34。使用 64(对于大多数部分不要重新分配,并在少数情况下获得上面提到的 2 的幂的好处)。

如果无法对此得出结论,或者至少很难这样做并且不是性能热点,那么您应该使用默认值。

This question came up today as a duplicate of another, but I notice one part wasn't answered. The default (assuming that means "when not created with a string that is large enough to require ) is 16 as people said, but I don't see anything here on when you should change it.

You change it when you can do so as a possible optimisation. Indeed, the choice of 16 is the opposite to an optimisation. Optimisation is picking values and approaches so as to particularly well suit a particular case or subset of possible cases, (not "making things faster" generally, though that's how we often use the word). Here the designer of the class had to deal with generalisation - picking values and approaches so as to give reasonably good performance across a broad range of cases.

The smaller they went, the less use of memory.

The larger they went, the less reallocation to deal with larger strings.

There's a few reasons why binary-round (whole powers of two) are likely to give better performance than other numbers in certain cases, so they went for one of those, but that aside the choice between 4 or 16 or 1024 was a matter of balancing different likely values.

Someone using StringBuilder rather than designing it, may have a better idea of what size they are likely to need.

If they are going to Append 5 1-digit numbers along with strings that total to 43 characters in length, then the total length of the StringBuilder is going to be 48 characters no matter what, so they should use a capacity of 48 as 48 is always the most efficient size for a string of 48 length.

If they are doing something where there could be any length between about 23 and 34 chars, they should use 34.

If they are doing something where there will likely never be more than 60 chars, but every now and then there could be, they should use 64 (don't reallocate for most parts, and gain the power-of-two benefit mentioned above for the few cases where you do).

If it's impossible to come to a conclusion on this, or at least hard to do so and not a performance hot-spot, then you should just use the default.

半城柳色半声笛 2024-07-15 05:47:02

J. Skeet 尊者针对这个问题提供了很好的分析:

https://jonskeet.uk/csharp /stringbuilder.html

The Venerable J. Skeet has provided a good analysis of precisely this problem:

https://jonskeet.uk/csharp/stringbuilder.html

三生一梦 2024-07-15 05:47:02

String-Builder 的默认容量是 16 个字符
String-Builder 的最大容量为2147483647 个字符

所以无需担心存储长响应!

Default capacity of a String-Builder is 16 characters ,
And Max capacity of String-Builder is 2147483647 characters.

So no need to worry for storing long response !!!

不必你懂 2024-07-15 05:47:02

我们可以使用StringBuilder类的capacity属性找到容量:

StringBuilder builder = new StringBuilder();
var capacity = builder.Capacity;
var maxCapacity = builder.MaxCapacity;

这里容量定义了StringBuilder的默认容量。

StringBuilderMaxCapacityInt32的最大值相同。

We can find the capacity using the capacity property of the StringBuilder Class:

StringBuilder builder = new StringBuilder();
var capacity = builder.Capacity;
var maxCapacity = builder.MaxCapacity;

Here capacity defines the default capacity of StringBuilder.

StringBuilder is Max Capacity is same as the maximum value of Int32.

初熏 2024-07-15 05:47:02

阅读有关 Stringbuilder 容量 的信息,有示例应用程序也来证明这一点。

read hear about Stringbuilder capacity , there is sample app to prove it too.

真心难拥有 2024-07-15 05:47:02

[编辑:当时,问题是关于 StringList]

你的意思是 StringCollection 吗? 最初使用空的 ArrayList,因此答案为 0。并且您无法更改它。 当您第一次添加一个项目时,容量会跳至 4,然后在填满时使用加倍策略。

如果您指的是 List,那么它是类似的(一个空的 T[],而不是 ArrayList),但是您可以初始化一个如果您需要的话,已知大小(即您知道您期望多少数据)。 同样,第一次 AddList 时,大小会跳至 4,然后每次填满时都会加倍。

[edit: at the time, the question asked about StringList]

Do you mean StringCollection? This uses an empty ArrayList initially, so the answer is 0. And you have no option to change it. When you first add an item, the capacity jumps to 4, then uses a doubling strategy when it fills.

If you mean List<string>, then it is similar (an empty T[], not an ArrayList), but you can initialize a known size if you need (i.e. you know how much data you expect). Again, the first time you Add to a List<T> the size jumps to 4, then doubles every time it fills up.

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