StringBuilder如何决定它的容量应该有多大?

发布于 2024-10-08 17:38:18 字数 243 浏览 0 评论 0原文

我知道当 sb 已满时使用 sb.Append(..) 时,StringBuilder 对象会分配更多内存。但容量会增加多少呢?

    StringBuilder sb = new StringBuilder(5);
    sb.Append("0123456789");

现在某人的能力是多少,为什么?什么是乘数?

只是为了清楚起见。我问的是容量而不是长度。

谢谢!

I know that the StringBuilder object allocates more memory when you use sb.Append(..) when the sb is already at capacity. But how much does that capacity increase?

    StringBuilder sb = new StringBuilder(5);
    sb.Append("0123456789");

Now what is the capacity of sb and why? What is the multiplier?

Just for clarity. I am asking about capacity and not length.

Thanks!

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

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

发布评论

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

评论(2

扮仙女 2024-10-15 17:38:18

除某些特殊情况外,容量每次都会加倍:

  • 如果加倍还不够,则容量会进一步增加到所需的确切数量。
  • 有一个上限 - 0x7fffffff。

您可以使用.NET Reflector或下载参考源来查看该算法。

我无法发布官方 .NET 实现的源代码,但这里是 Mono 实现的代码:

// Try double buffer, if that doesn't work, set the length as capacity
if (size > capacity) {

    // The first time a string is appended, we just set _cached_str
    // and _str to it. This allows us to do some optimizations.
    // Below, we take this into account.
    if ((object) _cached_str == (object) _str && capacity < constDefaultCapacity)
        capacity = constDefaultCapacity;

    capacity = capacity << 1;  // This means "capacity *= 2;"

    if (size > capacity)
        capacity = size;

    if (capacity >= Int32.MaxValue || capacity < 0)
        capacity = Int32.MaxValue;

    if (capacity > _maxCapacity && size <= _maxCapacity)
        capacity = _maxCapacity;
}

我还建议您不要编写依赖于此特定算法的代码,因为它是实现细节,而不是其他东西这是由接口保证的。

The capacity doubles each time apart from some special cases:

  • If doubling is not enough then the capacity is further increased to the exact amount that is required.
  • There is an upper limit - 0x7fffffff.

You can see the algorithm by using .NET Reflector or downloading the reference source.

I can't post the source code for the official .NET implementation but here's the code for the Mono implementation:

// Try double buffer, if that doesn't work, set the length as capacity
if (size > capacity) {

    // The first time a string is appended, we just set _cached_str
    // and _str to it. This allows us to do some optimizations.
    // Below, we take this into account.
    if ((object) _cached_str == (object) _str && capacity < constDefaultCapacity)
        capacity = constDefaultCapacity;

    capacity = capacity << 1;  // This means "capacity *= 2;"

    if (size > capacity)
        capacity = size;

    if (capacity >= Int32.MaxValue || capacity < 0)
        capacity = Int32.MaxValue;

    if (capacity > _maxCapacity && size <= _maxCapacity)
        capacity = _maxCapacity;
}

I would also recommend that you don't write code that relies on this specific algorithm as it is an implementation detail, and not something that is guaranteed by the interface.

梦旅人picnic 2024-10-15 17:38:18

它呈指数增长(具体来说,每次重新分配都会加倍),以便允许一系列追加花费 O(N) 时间而不是 O(N²) 时间。

It's exponential growth (specifically, doubling with each reallocation), in order to allow a series of appends to take O(N) time instead of O(N²) time.

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