问题描述
在,它说(隐含)的字符串连接反正优化成StringBuilder的操作,所以当我写我的code,没有任何理由来写的StringBuilder code源?请注意,我用例是从OP的问题不同,因为我是串联/追加行几百到几千。
In this answer, it says (implies) that String concatenation is optimised into StringBuilder operations anyway, so when I write my code, is there any reason to write StringBuilder code in the source? Note that my use case is different from the OP's question, as I am concatenating/appending hundreds-thousands of lines.
为了让自己更清楚:我清楚地感知每个的差异,只是我不知道它是否值得实际编写的StringBuilder code,因为它的可读性和当它理应更慢的表弟,字符串类,在编译过程中自动转换无妨。
To make myself clearer: I am well-aware about the differences of each, it's just that I don't know if it's worth actually writing StringBuilder code because it's less readable and when its supposedly slower cousin, the String class, is converted automagically in the compilation process anyway.
推荐答案
我觉得用的StringBuilder
的VS +
真的取决于你使用它的上下文。
I think the use of StringBuilder
vs +
really depends on the context you are using it in.
一般使用JDK 1.6和编译器将以上一起使用自动加入字符串的StringBuilder
。
Generally using JDK 1.6 and above the compiler will automatically join strings together using StringBuilder
.
String one = "abc";
String two = "xyz";
String three = one + two;
这将编译字符串3
为:
String three = new StringBuilder().append(one).append(two).toString();
这是非常有用,节省了我们一些运行时。然而,这种过程并不总是最佳的。举个例子:
This is quite helpful and saves us some runtime. However this process is not always optimal. Take for example:
String out = "";
for( int i = 0; i < 10000 ; i++ ) {
out = out + i;
}
return out;
如果我们编译成字节code,然后反编译字节code产生的,我们得到的东西,如:
If we compile to bytecode and then decompile the bytecode generated we get something like:
String out = "";
for( int i = 0; i < 10000; i++ ) {
out = new StringBuilder().append(out).append(i).toString();
}
return out;
编译器优化了内部循环,但肯定还没有做出最好的优化技术。为了提高我们code,我们可以使用:
The compiler has optimised the inner loop but certainly has not made the best possible optimisations. To improve our code we could use:
StringBuilder out = new StringBuilder();
for( int i = 0 ; i < 10000; i++ ) {
out.append(i);
}
return out.toString();
现在,这是比编译器生成的code更优化的,所以肯定是有需要用写code中的的StringBuilder
/ 的StringBuffer
在需要高效code类案件。目前的编译器是不是在循环处理连接字符串很大,但是这可能会在未来改变。
Now this is more optimal than the compiler generated code, so there is definitely a need to write code using the StringBuilder
/StringBuffer
classes in cases where efficient code is needed. The current compilers are not great at dealing concatenating strings in loops, however this could change in the future.
您需要仔细看看,看看你需要手动应用的StringBuilder
,并尝试使用它,它不会降低您的code的可读性了。
You need to look carefully to see where you need to manually apply StringBuilder
and try to use it where it will not reduce readability of your code too.
注:我编译使用JDK 1.6 code和并使用的javap
程序,它吐出字节code反编译code。这是相当容易的跨pret和经常看试图优化code当一个有用的参考。编译器会改变你在幕后code,因此它总是有趣的,看看它做什么!
Note: I compiled code using JDK 1.6, and and decompiled the code using the javap
program, which spits out byte code. It is fairly easy to interpret and is often a useful reference to look at when trying to optimise code. The compiler does change you code behind the scenes so it is always interesting to see what it does!
这篇关于Java的:使用concat VS StringBuilder的 - 进行了优化,所以我应该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!