C# StringBuilder 的默认容量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/246211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:42:00  来源:igfitidea点击:

Default capacity of StringBuilder

c#.netstringbuildercapacity

提问by Matt Lacey

What is the default capacity of a StringBuilder?

a 的默认容量是StringBuilder多少?

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

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

采纳答案by endian

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

尊敬的 J. Skeet 对这个问题进行了很好的分析:

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

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

回答by Marc Gravell

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

[编辑:当时,提出的问题是StringList]

Do you mean StringCollection? This uses an empty ArrayListinitially, 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.

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

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 Addto a List<T>the size jumps to 4, then doubles every time it fills up.

如果你的意思是List<string>,那么它是相似的(一个空的T[],不是一个ArrayList),但如果你需要,你可以初始化一个已知的大小(即你知道你期望多少数据)。同样,第一次Add进入 a 时List<T>,大小会跳到 4,然后每次填满时都会加倍。

回答by dove

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

阅读听说Stringbuilder capacity,也有示例应用程序来证明它。

回答by Alan

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

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

回答by baretta

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.

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

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.

我通常用某种粗略估计 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++ )
{
...
}

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.

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

回答by Jon Hanna

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.

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

You change it when you can do so as a possible optimisation. Indeed, the choice of 16 is the opposite to an optimisation. Optimisationis 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.

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

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.

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

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

有人使用StringBuilder,而不是设计它,可能他们是什么尺寸可能需要一个更好的主意。

If they are going to Append5 1-digit numbers along with strings that total to 43 characters in length, then the total length of the StringBuilderis 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.

如果他们要处理Append5 个 1 位数字以及总长度为 43 个字符的字符串,那么StringBuilder无论如何总长度将是 48 个字符,因此他们应该使用 48 的容量,因为 48 始终是长度为 48 的字符串的最有效大小。

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

如果他们正在做一些长度在大约 23 到 34 个字符之间的事情,他们应该使用 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).

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

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.

如果无法就此得出结论,或者至少很难得出结论而不是性能热点,那么您应该只使用默认值。

回答by user2761122

We can find the capacity using the capacity property of the StringBuilderClass:

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

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

Here capacity defines the default capacity of StringBuilder.

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

StringBuilderis Max Capacityis same as the maximum value of Int32.

StringBuilderMax Capacity是相同的最大值Int32

回答by Deep Jadia

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

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

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

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