是StringBuilder.append链比字符串连接更有效吗? [英] Is chain of StringBuilder.append more efficient than string concatenation?

查看:113
本文介绍了是StringBuilder.append链比字符串连接更有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据名为 Netbeans的提示,使用.append方法链而不是字符串连接

在调用StringBuilder或StringBuffer的append方法的参数中寻找字符串连接.

Looks for string concatenation in the parameter of an invocation of the append method of StringBuilder or StringBuffer.

StringBuilder.append()是否真的比字符串串联更有效?

Is StringBuilder.append() really more efficient than strings concatenation?

代码示例

StringBuilder sb = new StringBuilder();
sb.append(filename + "/");

vs.

StringBuilder sb = new StringBuilder();
sb.append(filename).append("/");

推荐答案

您必须在可读性和功能之间取得平衡.

You have to balance readability with functionality.

假设您具有以下条件:

String str = "foo";
str += "bar";
if(baz) str += "baz";

这将创建2个字符串生成器(实际上只需要1个),并为过渡创建一个附加的字符串对象.如果您去了,您会更有效率:

This will create 2 string builders (where you only need 1, really) plus an additional string object for the interim. You would be more efficient if you went:

StringBuilder strBuilder = new StringBuilder("foo");
strBuilder.append("bar");
if(baz) strBuilder.append("baz");
String str = strBuilder.toString();

但是从风格上来说,我认为第一个看起来还不错.对我来说,创建单个对象的性能优势似乎很小.现在,如果您有10个,20个或100个字符串,而不是3个字符串,那么我认为性能胜过样式.如果是循环的话,那么可以肯定我会使用字符串生成器,但是我认为只需几个字符串就可以使代码看起来更整洁.但是...里面有一个非常危险的陷阱!在下面的上阅读(暂停以建立悬念... dun dun dunnnn)

But as a matter of style, I think the first one looks just fine. The performance benefit of a single object creation seems very minimal to me. Now, if instead of 3 strings, you had 10, or 20, or 100, I would say the performance outweighs the style. If it was in a loop, for sure I'd use the string builder, but I think just a couple strings is fine to do the 'sloppy' way to make the code look cleaner. But... this has a very dangerous trap lurking in it! Read on below (pause to build suspense... dun dun dunnnn)

有些人说总是使用显式字符串生成器.一个基本原理是您的代码将继续增长,并且通常会以与现在相同的方式进行增长(即,它们将不需要花费时间进行重构.)因此,您最终要创建每条创建的10或20条语句自己的构建器,当您不需要时.因此从一开始就防止这种情况发生,他们说始终使用显式生成器.

There are those who say to always use the explicit string builder. One rationale is that your code will continue to grow, and it will usually do so in the same manner as it is already (i.e. they won't take the time to refactor.) So you end up with those 10 or 20 statements each creating their own builder when you don't need to. So to prevent this from the start, they say always use an explicit builder.

因此,在您的示例中,它并不会特别快,当将来某人决定结束时想要文件扩展名或类似的名称时,如果他们继续使用字符串连接而不是StringBuilder,他们会最终会遇到性能问题.

So while in your example, it's not going to be particularly faster, when someone in the future decides they want a file extension on the end, or something like that, if they continue to use string concatenation instead of a StringBuilder, they're going to run into performance problems eventually.

我们还需要考虑未来.假设您正在用JDK 1.1重新制作Java代码,并且具有以下方法:

We also need to think about the future. Let's say you were making Java code back in JDK 1.1 and you had the following method:

public String concat(String s1, String s2, String s3) {
    return s1 + s2 + s3;
}

那时候,这会很慢,因为StringBuilder不存在.

At that time, it would have been slow because StringBuilder didn't exist.

然后在JDK 1.3中,您决定通过使用StringBuffer(StringBuilder尚不存在)使其更快.您可以这样做:

Then in JDK 1.3 you decided to make it faster by using StringBuffer (StringBuilder still doesn't exist yet). You do this:

public String concat(String s1, String s2, String s3) {
    StringBuffer sb = new StringBuffer();
    sb.append(s1);
    sb.append(s2);
    sb.append(s3);
    return sb.toString();
}

它变得更快.太棒了!

现在出现了JDK 1.5,并附带了StringBuilder(比StringBuffer快)和自动转换

Now JDK 1.5 comes out, and with it comes StringBuilder (which is faster than StringBuffer) and the automatic transation of

return s1 + s2 + s3;

return new StringBuilder().append(s1).append(s2).append(s3).toString();

但是,由于显式使用StringBuffer,因此无法获得此性能优势.因此,通过变得聪明,当Java比您聪明时,您会导致性能下降.因此,您必须记住,有些事情是您不会想到的.

But you don't get this performance benefit because you're using StringBuffer explicitly. So by being smart, you have caused a performance hit when Java got smarter than you. So you have to keep in mind that there are things out there you won't think of.

这篇关于是StringBuilder.append链比字符串连接更有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆