最佳实践/性能:将StringBuilder.append与String.concat混合使用 [英] Best practices/performance: mixing StringBuilder.append with String.concat
问题描述
我试图了解最佳实践是什么以及为什么将字符串文字和变量连接起来用于不同的情况。例如,如果我有这样的代码
I'm trying to understand what the best practice is and why for concatenating string literals and variables for different cases. For instance, if I have code like this
StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA")
.append(B_String).append("CCCCCCCCCCC").append(D_String)
.append("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
.append("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
这是这样做的吗?从这篇文章中,我注意到 +
string上的运算符创建一个StringBuilder的新实例,连接操作数,并返回一个String转换,这似乎比调用 .append()
更多的工作;所以,如果这是真的,那就不可能了。但是 String.concat()
呢?每次连接使用 .append()
是否合适?或者只是变量,文字可以附加 .concat()
?
Is this the way to do it? From this post, I noticed that the +
operator on Strings creates a new instance of StringBuilder, concatenates the operands, and returns a String conversion, which seems like a lot more work than just calling .append()
; so if that's true, then that is out of the question. But what about String.concat()
? Is it proper to use .append()
for every concatenation? Or just for variables, and literals are okay to append with .concat()
?
StringBuilder sb = new StringBuilder("AAAAAAAAAAAAA")
.append(B_String.concat("CCCCCCCCCCC")).append(D_String
.concat("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE")
.concat("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"));
针对这些情况,最佳做法和表现的一般规则是什么?我的假设在 +
上是否正确,它应该真的不被使用?
What are the general rules for best practices and performance for going about these situations? Is my assumption correct on +
and it should really just not be used?
推荐答案
+
operator
+
operator
String s = s1 + s2
在幕后,这被翻译为:
String s = new StringBuilder(s1).append(s2).toString();
想象一下,如果你有 s1 + s2,它会增加多少额外工作
这里:
Imagine how much extra work it adds if you have s1 + s2
here:
stringBuilder.append(s1 + s2)
而不是:
stringBuilder.append(s1).append(s2)
多个字符串 +
值得注意的是:
Multiple strings with +
Worth to note that:
String s = s1 + s2 + s3 + ... +sN
被翻译为:
String s = new StringBuilder(s1).append(s2).append(s3)...apend(sN).toString();
concat()
concat()
String s = s1.concat(s2);
String
创建 char []
可以同时适合 s1
和 s2
的数组。将 s1
和 s2
的内容复制到此新阵列。实际上需要更少的工作,然后 +
运算符。
String
creates char[]
array that can fit both s1
and s2
. Copies s1
and s2
contents to this new array. Actually requires less work then +
operator.
维护一个内部 char []
数组,该数组在需要时增长。如果内部足够大,则不会创建额外的 char []
。
Maintains an internal char[]
array that grows when needed. No extra char[]
is created if the internal one is sufficiently big.
stringBuilder.append(s1.concat(s2))
也表现不佳,因为 s1.concat(s2)
创建一个额外的 char []
数组并复制 s1
和 s2
只是为了将新的数组内容复制到内部 StringBuilder
char [ ]
。
is also performing poorly because s1.concat(s2)
creates an extra char[]
array and copies s1
and s2
to it just to copy that new array contents to internal StringBuilder
char[]
.
据说你应该一直使用 append()
附加原始字符串(您的第一个代码段是正确的)。
That being said you should use append()
all the time and append raw strings (your first code snippet is correct).
这篇关于最佳实践/性能:将StringBuilder.append与String.concat混合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!