Java String.format() 与字符串连接性能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21805557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String.format() vs string concatenation performance
提问by Leo
is there any difference in performance between these two idioms ?
这两个习语在性能上有什么区别吗?
String firstStr = "Hello ";
String secStr = "world";
String third = firstStr + secStr;
and
和
String firstStr = "Hello ";
String secStr = "world";
String third = String.format("%s%s",firstStr , secStr);
I know that concatenation with + operator is bad for performance specially if the operation is done a lot of times, but what about String.format() ? is it the same or it can help to improve performance?
我知道与 + 运算符的连接对性能不利,特别是如果操作进行了很多次,那么 String.format() 呢?它是一样的还是可以帮助提高性能?
采纳答案by peter.petrov
The second one will be even slower (if you look at the source code of String.format()
you will see why). It is just because String.format()
executes much more code than the simple concatenation. And at the end of the day, both code versions create 3 instances of String
. There are other reasons, not performance related, to use String.format()
, as others already pointed out.
第二个会更慢(如果你查看源代码String.format()
你就会明白为什么)。这只是因为String.format()
执行的代码比简单的连接多得多。最终,两个代码版本都创建了 3 个String
. String.format()
正如其他人已经指出的那样,还有其他与性能无关的原因要使用。
回答by Coffee
As described in this excellent answer, you would rather use String.format, but mainly because of localization concerns.
如这个优秀答案中所述,您宁愿使用 String.format,但主要是因为本地化问题。
Suppose that you had to supply different text for different languages, in that case then using String.format - you can just plug in the new languages(using resource files). But concatenation leaves messy code.
假设您必须为不同的语言提供不同的文本,在这种情况下使用 String.format - 您可以插入新语言(使用资源文件)。但是串联会留下混乱的代码。
See : Is it better practice to use String.format over string Concatenation in Java?
回答by Dolda2000
First of all, let me just put a disclaimer against premature optimization. Unless you are reasonably sure this is going to be a hotspot in your program, just choose the construct that fits your program the best.
首先,让我免责声明反对过早优化。除非您有理由确定这将成为您程序中的热点,否则只需选择最适合您的程序的结构即可。
If you arereasonably sure, however, and want to have good control over the concatenation, just use a StringBuilder
directly. That's what the built-in concatenation operation does anyway, and there's no reason to assume that it's slow. As long as you keep the same StringBuilder
and keep appending to it, rather than risking creating several in a row (which would have to be "initialized" with the previously created data), you'll have proper O(n) performance. Especially so if you make sure to initialize the StringBuilder
with proper capacity.
如果你是有理由相信,但是,要加强对级联控制好,只使用一个StringBuilder
直接。无论如何,这就是内置连接操作所做的,并且没有理由假设它很慢。只要您保持相同StringBuilder
并继续附加到它,而不是冒着连续创建多个的风险(必须使用先前创建的数据“初始化”),您将获得适当的 O(n) 性能。特别是如果您确保以StringBuilder
适当的容量初始化。
That also said, however, a StringBuilder
is, as mentioned, what the built-in concatenation operation uses anyway, so if you just keep all your concatenations "inline" -- that is, use A + B + C + D
, rather than something like e = A + B
followed by f = C + D
followed by e + f
(this way, the same StringBuilder
is used and appended to throughout the entire operation) -- then there's no reason to assume it would be slow.
然而,这也说,StringBuilder
正如所提到的,a是内置连接操作无论如何都要使用的,所以如果你只是保持所有的连接“内联”——也就是说,使用A + B + C + D
, 而不是像e = A + B
后跟f = C + D
后跟e + f
(this方式,在StringBuilder
整个操作中使用并附加相同的内容) - 那么没有理由假设它会很慢。
EDIT:In reply to your comment, I'd say that String.format
is always slower. Even if it appends optimally, it can't do it any fasterthan StringBuilder
(and therefore also the concatenation operation) does anyway, but it also has to create a Formatter
object, do parsing of the input string, and so on. So there's more to it, but it still can't do the basic operation any faster.
编辑:在回复您的评论时,我会说这String.format
总是较慢。即使它以最佳方式追加,它也不能比(因此也是连接操作)更快地完成它StringBuilder
,但它也必须创建一个Formatter
对象,解析输入字符串,等等。所以它还有更多,但它仍然无法更快地完成基本操作。
Also, if you look internally in how the Formatter
works, you'll find that it also (by default) uses a StringBuilder
, just as the concatenation operation does. Therefore, it does the exact same basic operation -- that is, feeding a StringBuilder
with the strings you give it. It just does it in a far more roundabout way.
此外,如果您从内部查看其Formatter
工作原理,您会发现它也(默认情况下)使用 a StringBuilder
,就像连接操作一样。因此,它执行完全相同的基本操作——也就是说,StringBuilder
用你给它的字符串输入a 。它只是以一种更加迂回的方式来实现。