简体   繁体   English

String.format()vs字符串连接性能

[英]String.format() vs string concatenation performance

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() ? 我知道用+运算符连接对性能有害,特别是如果操作很多次,但是String.format()呢? is it the same or it can help to improve performance? 它是相同的还是有助于提高性能?

The second one will be even slower (if you look at the source code of String.format() you will see why). 第二个将更慢(如果您查看String.format()的源代码,您将看到原因)。 It is just because String.format() executes much more code than the simple concatenation. 这只是因为String.format()执行的代码比简单的连接要多得多。 And at the end of the day, both code versions create 3 instances of String . 在一天结束时,两个代码版本都会创建3个String实例。 There are other reasons, not performance related, to use String.format() , as others already pointed out. 正如其他人已经指出的那样,使用String.format()还有其他原因,而不是性能相关。

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 are reasonably sure, however, and want to have good control over the concatenation, just use a StringBuilder directly. 如果你有理由相信,但是,要加强对拼接好的控制,只需使用StringBuilder直接。 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. 只要你保持相同的StringBuilder并继续附加它,而不是冒险连续创建几个(必须用先前创建的数据“初始化”),你将有适当的O(n)性能。 Especially so if you make sure to initialize the StringBuilder with proper capacity. 特别是如果你确保用适当的容量初始化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 + B + C + D ,而不是比如e = A + B然后是f = C + D然后是e + f (这样,在整个操作过程中使用相同的StringBuilder并附加) - 然后就没有理由认为它会很慢。

EDIT: In reply to your comment, I'd say that String.format is always slower. 编辑:在回复你的评论时,我会说String.format总是比较慢。 Even if it appends optimally, it can't do it any faster than 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. 即使它以最佳方式附加,它也不能比StringBuilder 更快 (因此也是串联操作),但它还必须创建一个Formatter对象,解析输入字符串,等等。 So there's more to it, but it still can't do the basic operation any faster. 所以它还有更多,但它仍然无法更快地完成基本操作。

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. 此外,如果您在内部查看Formatter工作方式,您会发现它(默认情况下)也使用StringBuilder ,就像连接操作一样。 Therefore, it does the exact same basic operation -- that is, feeding a StringBuilder with the strings you give it. 因此,它执行完全相同的基本操作 - StringBuilder提供您提供的字符串。 It just does it in a far more roundabout way. 它只是以更加迂回的方式进行。

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). 假设你必须为不同的语言提供不同的文本,在这种情况下然后使用String.format - 你可以插入新的语言(使用资源文件)。 But concatenation leaves messy code. 但连接会留下凌乱的代码。

See : Is it better practice to use String.format over string Concatenation in Java? 请参阅: 在Java中使用String.format而不是字符串连接更好吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM