字符串连接是否使用+优化到.NET中的StringBuilder实现?(Are string concatenations using + optimized to a StringBuilder implementation in .NET?)

我已经阅读了多个地方,在Java 1.5+ String连接被优化为在编译程序时使用StringBuilder 。 我不清楚这是一个标准还是只是许多编译器采用的常见优化。 关于这一点的任何澄清都将受到赞赏,但主要是它是我的第二个问题的引导。

.NET是否同样优化? 我知道如果我使用StringBuilder这将消除任何歧义,但我个人觉得简单+更容易阅读。 如果是.NET,那么这是从特定版本开始的吗? 精心制作表示赞赏。

I've read multiple places that in Java 1.5+ String concatenations are optimized to using a StringBuilder when a program is compiled. It's unclear to me if this is a standard or just a common optimization many compilers employ. Any clarificion in regards to this would be appreciated, but mainly it's a lead-in to my second question.

Does .NET similarly optimize? I'm aware that if I use StringBuilder this will eliminate any ambiguity but I personally find the simplicity of + easier to read. If .NET does, did this start in a specific version? Elaboration is appreciated.

最满意答案

希望这会让你更好地了解StringBuilder,然后用+表示字符串

性能注意事项

String对象并置操作始终从现有字符串和新数据创建新对象。 StringBuilder对象维护一个缓冲区以容纳新数据的连接。 如果房间可用,则将新数据附加到缓冲区; 否则,分配一个新的较大缓冲区,将原始缓冲区中的数据复制到新缓冲区,并将新数据附加到新缓冲区。

String或StringBuilder对象的串联操作的性能取决于内存分配的频率。 字符串连接操作始终分配内存,而StringBuilder连接操作仅在StringBuilder对象缓冲区太小而无法容纳新数据时才分配内存。 如果要连接固定数量的String对象,请使用String类。 在这种情况下,编译器甚至可以将单个级联操作组合到单个操作中。 如果要连接任意数量的字符串,请使用StringBuilder对象; 例如,如果您使用循环来连接随机数量的用户输入字符串。

MSDN

Hope this will give you a better view towards StringBuilder then string conctenation with +

Performance Considerations

A String object concatenation operation always creates a new object from the existing string and the new data. A StringBuilder object maintains a buffer to accommodate the concatenation of new data. New data is appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, and the new data is appended to the new buffer.

The performance of a concatenation operation for a String or StringBuilder object depends on the frequency of memory allocations. A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation allocates memory only if the StringBuilder object buffer is too small to accommodate the new data. Use the String class if you are concatenating a fixed number of String objects. In that case, the compiler may even combine individual concatenation operations into a single operation. Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you're using a loop to concatenate a random number of strings of user input.

MSDN

字符串连接是否使用+优化到.NET中的StringBuilder实现?(Are string concatenations using + optimized to a StringBuilder implementation in .NET?)

我已经阅读了多个地方,在Java 1.5+ String连接被优化为在编译程序时使用StringBuilder 。 我不清楚这是一个标准还是只是许多编译器采用的常见优化。 关于这一点的任何澄清都将受到赞赏,但主要是它是我的第二个问题的引导。

.NET是否同样优化? 我知道如果我使用StringBuilder这将消除任何歧义,但我个人觉得简单+更容易阅读。 如果是.NET,那么这是从特定版本开始的吗? 精心制作表示赞赏。

I've read multiple places that in Java 1.5+ String concatenations are optimized to using a StringBuilder when a program is compiled. It's unclear to me if this is a standard or just a common optimization many compilers employ. Any clarificion in regards to this would be appreciated, but mainly it's a lead-in to my second question.

Does .NET similarly optimize? I'm aware that if I use StringBuilder this will eliminate any ambiguity but I personally find the simplicity of + easier to read. If .NET does, did this start in a specific version? Elaboration is appreciated.

最满意答案

希望这会让你更好地了解StringBuilder,然后用+表示字符串

性能注意事项

String对象并置操作始终从现有字符串和新数据创建新对象。 StringBuilder对象维护一个缓冲区以容纳新数据的连接。 如果房间可用,则将新数据附加到缓冲区; 否则,分配一个新的较大缓冲区,将原始缓冲区中的数据复制到新缓冲区,并将新数据附加到新缓冲区。

String或StringBuilder对象的串联操作的性能取决于内存分配的频率。 字符串连接操作始终分配内存,而StringBuilder连接操作仅在StringBuilder对象缓冲区太小而无法容纳新数据时才分配内存。 如果要连接固定数量的String对象,请使用String类。 在这种情况下,编译器甚至可以将单个级联操作组合到单个操作中。 如果要连接任意数量的字符串,请使用StringBuilder对象; 例如,如果您使用循环来连接随机数量的用户输入字符串。

MSDN

Hope this will give you a better view towards StringBuilder then string conctenation with +

Performance Considerations

A String object concatenation operation always creates a new object from the existing string and the new data. A StringBuilder object maintains a buffer to accommodate the concatenation of new data. New data is appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, and the new data is appended to the new buffer.

The performance of a concatenation operation for a String or StringBuilder object depends on the frequency of memory allocations. A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation allocates memory only if the StringBuilder object buffer is too small to accommodate the new data. Use the String class if you are concatenating a fixed number of String objects. In that case, the compiler may even combine individual concatenation operations into a single operation. Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you're using a loop to concatenate a random number of strings of user input.

MSDN