如何StringBuilder的工作? [英] How does StringBuilder work?

查看:94
本文介绍了如何StringBuilder的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何的StringBuilder 工作?

这是什么做的内部的?是否使用不安全的code? 为什么会这样快(比 + 运营商)?

What does it do internally? Does it use unsafe code? And why is it so fast (compared to the + operator)?

推荐答案

当您使用+运算符来建立一个字符串:

When you use the + operator to build up a string:

string s = "01";
s += "02";
s += "03";
s += "04";

然后在第一个串联我们做长度为4的一个新的字符串,并复制01和02到它 - 四个大字被复制。在第二级联我们做长六一个新的字符串,并复制0102和03到它 - 六个字符复制。在第三CONCAT,我们做长八串并复制010203和04到它 - 八个字被复制。到目前为止总共4 + 6 + 8 = 18个字符已被复制为这八个字符串。继续前进。

then on the first concatenation we make a new string of length four and copy "01" and "02" into it -- four characters are copied. On the second concatenation we make a new string of length six and copy "0102" and "03" into it -- six characters are copied. On the third concat, we make a string of length eight and copy "010203" and "04" into it -- eight characters are copied. So far a total of 4 + 6 + 8 = 18 characters have been copied for this eight-character string. Keep going.

...
s += "99";

在第98 CONCAT我们做长度198的字符串,并复制010203 ...... 98和99到它。这给了我们一共有4 + 6 + 8 + ... + 198 =很多,为​​了让这个198字符的字符串。

On the 98th concat we make a string of length 198 and copy "010203...98" and "99" into it. That gives us a total of 4 + 6 + 8 + ... + 198 = a lot, in order to make this 198 character string.

一个字符串生成器,不会做所有的复制。相反,它认为是希望为比最终串大的可变数组,和充塞新事物到数组是必要的。

A string builder doesn't do all that copying. Rather, it maintains a mutable array that is hoped to be larger than the final string, and stuffs new things into the array as necessary.

在猜测是错误的,该阵列得到充分,会发生什么?有两种策略。在该框架的previous版本,字符串生成器重新分配并复制该阵列时,它得到了充分和规模扩大了一倍。在新的实现中,字符串生成器保持相对小数组的链表,并追加新的数组到列表的末尾,当旧已满。

What happens when the guess is wrong and the array gets full? There are two strategies. In the previous version of the framework, the string builder reallocated and copied the array when it got full, and doubled its size. In the new implementation, the string builder maintains a linked list of relatively small arrays, and appends a new array onto the end of the list when the old one gets full.

此外,正如你所猜测的字符串生成器可以做到的技巧与不安全code,以改善其性能。例如,code其中写入新数据到阵列可已经检查了阵列写入将是在限制范围内。通过关闭安全系统这样可避免每次写入检查的抖动可能另外插入来验证每写入阵列是安全的。该字符串生成器做了一些这些各种各样的技巧,做这样的事情,确保缓冲被重用,而不是重新分配,以确保必要的安全检查,避免等。我建议对这类把戏,除非你是在正确输入不安全code真的很好,而且确实需要勉强维持的性能每一个最后一位。

Also, as you have conjectured, the string builder can do tricks with "unsafe" code to improve its performance. For example, the code which writes the new data into the array can already have checked that the array write is going to be within bounds. By turning off the safety system it can avoid the per-write check that the jitter might otherwise insert to verify that every write to the array is safe. The string builder does a number of these sorts of tricks to do things like ensuring that buffers are reused rather than reallocated, ensuring that unnecessary safety checks are avoided, and so on. I recommend against these sorts of shenanigans unless you are really good at writing unsafe code correctly, and really do need to eke out every last bit of performance.

这篇关于如何StringBuilder的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆