+= 比 concat 更高效吗?
我一直在阅读我团队中其他开发人员编写的代码,他们似乎更喜欢使用 +=
进行字符串连接,而我更喜欢使用 .concat()
,因为它感觉更容易阅读。
我正在尝试准备一个关于为什么使用 .concat()
更好的论点,我想知道,两者之间的效率有什么区别吗?
我们“应该”采取哪个选项?
public class Stuff {
public static void main(String[] args) {
String hello = "hello ";
hello += "world";
System.out.println(hello);
String helloConcat = "hello ".concat("world");
System.out.println(helloConcat);
}
}
I've been reading code produced by other developers on my team, they seem to favor using +=
for String concatenation, whereas I prefer using .concat()
as it feels easier to read.
I'm trying to prepare an argument as to why using .concat()
is better, and I'm wondering, is there any difference in efficiency between the two?
Which option "should" we be taking?
public class Stuff {
public static void main(String[] args) {
String hello = "hello ";
hello += "world";
System.out.println(hello);
String helloConcat = "hello ".concat("world");
System.out.println(helloConcat);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
就可读性而言,我认为你是非常错误的。
+
胜过.concat()
。如果您使用的是+=
,您可能需要考虑StringBuilder.append
为循环保留相同的StringBuilder
。就性能而言,
concat
优于+
。只要您只使用一个或两个即可。对于
concat
,您最终将创建一个具有正确大小的char[]
的String
对象。这是你能得到的尽可能最佳的结果。对于
+
javac 生成的代码构造StringBuilder
来执行附加操作,然后转换为String
。从 1.5 开始,您创建:StringBuilder
(浪费)StringBuilder
的初始char[]
(浪费)char[]
(浪费)String
。String
的char[]
。但是,您很少看到使用
concat
,因为它更难以阅读。与其他正在发生的事情相比,几乎可以肯定,性能只是沧海一粟(提示:在优化之前先尝试一下信封背面)。In terms of readability, I think you are very wrong.
+
wins over.concat()
. If you are using+=
, you might want to think aboutStringBuilder.append
keeping the sameStringBuilder
for the loop.In terms of performance
concat
is better than+
. so long as you only use one or perhaps two.In the case of
concat
, you will end up creating aString
object with a correctly sizedchar[]
. It's about as optimal as you can get.For
+
javac generates code that constructs aStringBuilder
to do the appending and then converts to aString
. From 1.5 you create:StringBuilder
(waste)char[]
forStringBuilder
(waste)char[]
(waste)String
.String
'schar[]
.However, you rarely see
concat
used because it is more difficult to read. The performance is almost certainly going to be a drop in the ocean compared to what else is going on (hint: try the back of an envelope before optimising).你应该做你认为最短、最清晰的事情。
不过,为了你的兴趣。
是最快的,因为编译将两个字符串合并为一个。
这对于两个字符串来说是最快的,因为它避免了创建 StringBuilder。
但是,如果使用 StringBuilder 有两个以上的字符串,则隐式或显式的速度最快。
总之,我会使用 + 除非你将它放在循环中,在这种情况下我可能会显式使用 StringBuilder 来提高性能。
You should do what you find is the shortest and clearest to you.
However, for your interest.
Is the fastest as the compile combined the two String into one.
This is fastest for two Strings as it avoids creating StringBuilder.
However if you have more than two strings Using StringBuilder, either implicitly or explicitly is fastest.
In summary I would use + unless you have it in a loop in which case I might use StringBuilder explicitly to improve performance.
Concat对于两个字符串连接来说绝对是更快的选择,我不知道为什么javac内部使用
而不是
for s1 += s2。请参阅我对类似问题的回答连接运算符 (+) 与 concat()
Concat is definitely a faster choice for two strings concatination, I don't know why javac internally uses
instead of
for s1 += s2. See my answer to a similar question concatenation operator (+) vs concat()
我找到了这个 artical 并做了这个测试:
它看起来像
StringBuilder
最快,+=
最慢。仅供参考!
I have found this artical and made this test :
It looks like that
StringBuilder
is fastest and+=
is slowest.Just for reference!
由于 String 在 java 中是不可变的,因此当您执行
+
、+=
或concat(String)
时,会生成一个新的 String。字符串越大,花费的时间就越长——需要复制的内容就越多,产生的垃圾也就越多。当今的 java 编译器会优化字符串连接以使其达到最佳状态,例如
编译器将其生成为:
我的建议是编写更易于维护和阅读的代码。
此链接显示了 StringBuilder 与 StringBuffer 与 String.concat 的性能 - 做得正确
Since String is immutable in java, when you do a
+
,+=
orconcat(String)
, a new String is generated. The bigger the String gets the longer it takes - there is more to copy and more garbage is produced.Today's java compilers optimizes your string concatenation to make it optimal, e.g.
Compiler generates it to:
My advice is to write code that's easier to maintain and read.
This link shows performance of StringBuilder vs StringBuffer vs String.concat - done right
应该没关系。现代 Java 编译器、JVM 和 JIT 将以差异最小化的方式优化您的代码。您应该努力编写对您来说更具可读性和可维护性的代码。
It shouldn't matter. Modern day Java compilers, JVMs and JITs will optimize your code in such a way that differences are likely to be minimal. You should strive to write code that's more readable and maintainable for you.
我同意@darioo 和大多数其他答案。始终将可读性(和可维护性)放在第一位。 (现代 JIT 编译器应该不会遇到像这样的简单情况的问题。)
然而,这是与您的程序相对应的字节码。 (请注意,
+=
方法会生成StringBuilder
,这通常是构造字符串时的首选方法。)I agree with @darioo and most other answers. Always put readability (and maintainability) first. (A modern JIT compiler should have no troubles with simple cases like these.)
Here is however the bytecode corresponding to your program. (Note that the
+=
approach results in aStringBuilder
which is generally the preferred approach when constructing strings.)