为什么String连接比Java中的StringBuilder更快?

时间:2017-05-29 09:59:07

标签: java string stringbuilder

该程序将对象的状态转换为HTML字符串。

public class Test {
    public static void main(String[] args) {
        Address addr = new Address();
        addr.setLine1("A straight line");
        addr.setLine2("A curve");
        addr.setCity("A Round City");
        addr.setState("A Triangular State");
        addr.setCountry("A Rectangle Country");
        addr.setZip("123456");

        @SuppressWarnings("unused")
        String str;
        int count = 1000;
        for (int j = 0; j < 5; j++) {

            double timeRich = System.nanoTime();
            for (int i = 0; i < count; i++) {
                str = AddressFormatter.formatRich(addr);
            }
            timeRich = System.nanoTime() - timeRich;

            double timeFine = System.nanoTime();
            for (int i = 0; i < count; i++) {
                str = AddressFormatter.formatFine(addr);
            }
            timeFine = System.nanoTime() - timeFine;


            double timePoor = System.nanoTime();
            for (int i = 0; i < count; i++) {
                str = AddressFormatter.formatPoor(addr);
            }
            timePoor = System.nanoTime() - timePoor;

            System.out.println("Test cases: " + count);
            System.out.println("Average time to format (SB Poor): " + (int)(timePoor/count) + " ns");
            System.out.println("Average time to format (SB Fine): " + (int)(timeFine/count) + " ns");
            System.out.println("Average time to format (String) : " + (int)(timeRich/count) + " ns");
            System.out.println();
            count *= 10;
        }
        System.out.println("***End of test***");
    }
}

class Address {
    private String line1;
    private String line2;
    private String city;
    private String state;
    private String country;
    private String zip;

    /**
     * Default constructor.
     */
    public Address() {}

    public String getLine1() {
        return line1;
    }
    public void setLine1(String line1) {
        this.line1 = line1;
    }
    public String getLine2() {
        return line2;
    }
    public void setLine2(String line2) {
        this.line2 = line2;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getZip() {
        return zip;
    }
    public void setZip(String zip) {
        this.zip = zip;
    }
}

class AddressFormatter {
    // more readable than formatFine()
    public static String formatPoor(Address obj) {
        StringBuilder str = new StringBuilder();
        str.append("<div class=\"address-wrapper\">\n");
        str.append("\t<div class=\"addr-line\">" + obj.getLine1() + "</div>\n");
        str.append("\t<div class=\"addr-line\">" + obj.getLine2() + "</div>\n");
        str.append("\t<div class=\"addr-city\">" + obj.getCity() + "</div>\n");
        str.append("\t<div class=\"addr-state\">" + obj.getState() + "</div>\n");
        str.append("\t<div class=\"addr-country\">" + obj.getCountry() + "</div>\n");
        str.append("\t<div class=\"addr-zip\">" + obj.getZip() + "</div>\n");
        str.append("</div>\n");

        return str.toString();
    }

    // grouping all constants, removing string concatenations
    public static String formatFine(Address obj) {
        StringBuilder str = new StringBuilder();
        str.append("<div class=\"address-wrapper\">\n\t<div class=\"addr-line\">");
        str.append(obj.getLine1());
        str.append("</div>\n\t<div class=\"addr-line\">");
        str.append(obj.getLine2());
        str.append("</div>\n\t<div class=\"addr-city\">");
        str.append(obj.getCity());
        str.append("</div>\n\t<div class=\"addr-state\">");
        str.append(obj.getState());
        str.append("</div>\n\t<div class=\"addr-country\">");
        str.append(obj.getCountry());
        str.append("</div>\n\t<div class=\"addr-zip\">");
        str.append(obj.getZip());
        str.append("</div>\n</div>\n");

        return str.toString();
    }

    public static String formatRich(Address obj) {
        return "<div class=\"address-wrapper\">\n"
        + "\t<div class=\"addr-line\">" + obj.getLine1() + "</div>\n"
        + "\t<div class=\"addr-line\">" + obj.getLine2() + "</div>\n"
        + "\t<div class=\"addr-city\">" + obj.getCity() + "</div>\n"
        + "\t<div class=\"addr-state\">" + obj.getState() + "</div>\n"
        + "\t<div class=\"addr-country\">" + obj.getCountry() + "</div>\n"
        + "\t<div class=\"addr-zip\">" + obj.getZip() + "</div>\n"
        + "</div>\n";
    }
}

在Eclipse中运行此程序时,我得到以下结果:

Test cases: 1000
Average time to format (SB Poor): 13513 ns
Average time to format (SB Fine): 7052 ns
Average time to format (String) : 14088 ns

Test cases: 10000
Average time to format (SB Poor): 3061 ns
Average time to format (SB Fine): 3290 ns
Average time to format (String) : 1618 ns

Test cases: 100000
Average time to format (SB Poor): 3486 ns
Average time to format (SB Fine): 1568 ns
Average time to format (String) : 589 ns

Test cases: 1000000
Average time to format (SB Poor): 616 ns
Average time to format (SB Fine): 547 ns
Average time to format (String) : 497 ns

Test cases: 10000000
Average time to format (SB Poor): 657 ns
Average time to format (SB Fine): 626 ns
Average time to format (String) : 191 ns

***End of test***

为什么String版本比StringBuilder版本更快?

为什么每次迭代后平均时间都在减少?

编辑: 我通过删除'StringBuilder'版本中的所有连接操作添加了另一个格式化函数(如一个答案所指出的)。

在第一次迭代中,'String'版本是最慢的。

在最后一次迭代中,'String'版本是最快的。

2 个答案:

答案 0 :(得分:3)

问题的第二部分很简单:JVM识别重复执行并优化机器代码,这就是it's important to handle benchmarks carefully的原因。

以下是解释实施方面差异的原因:

你的&#34; StringBuilder&#34;实现写得很差。您没有附加每个组件,而是执行字符串连接(为每个方法调用创建然后丢弃新的StringBuilder ,然后附加结果。如果您为每个元素正确使用了.append,那么您可以看到更少的差异。

但是,现代Java编译器将一系列与+的字符串连接转换为隐式StringBuilder调用,以最大限度地减少对象创建。 Java编译器还合并连接的编译时字符串常量。因此,您的format2方法使用StringBuilder,但有一个重要区别 - 所有行包装的相邻字符串常量都已合并。因此,即使您要修复format方法,format2也会更快,因为它会对更多固定内容进行分组。

答案 1 :(得分:1)

我预计formatRichformatFine的字节代码是等价的,但事实并非如此。所以我试着得到两个等价的方法:

将StringBuilder方法重写为

public static String formatFine(Address obj) {
    return new StringBuilder("<div class=\"address-wrapper\">\n\t<div class=\"addr-line\">")
        .append(obj.getLine1())
        .append("</div>\n\t<div class=\"addr-line\">")
        .append(obj.getLine2())
        .append("</div>\n\t<div class=\"addr-city\">")
        .append(obj.getCity())
        .append("</div>\n\t<div class=\"addr-state\">")
        .append(obj.getState())
        .append("</div>\n\t<div class=\"addr-country\">")
        .append(obj.getCountry())
        .append("</div>\n\t<div class=\"addr-zip\">")
        .append(obj.getZip())
        .append("</div>\n</div>\n").toString();
}

此方法相当于以下java字节代码:

public static String formatRich(Address obj) {
    return "<div class=\"address-wrapper\">\n\t<div class=\"addr-line\">"
        + obj.getLine1()
        + "</div>\n\t<div class=\"addr-line\">"
        + obj.getLine2()
        + "</div>\n\t<div class=\"addr-city\">"
        + obj.getCity()
        + "</div>\n\t<div class=\"addr-state\">"
        + obj.getState()
        + "</div>\n\t<div class=\"addr-country\">"
        + obj.getCountry()
        + "</div>\n\t<div class=\"addr-zip\">"
        + obj.getZip()
        + "</div>\n</div>\n";
}

执行主程序会解析(在我的机器上):

...

Test cases: 10000000
Average time to format (SB Poor): 633 ns
Average time to format (SB Fine): 151 ns
Average time to format (String) : 152 ns

说明:

  • 显式语句str.append必须从堆栈加载str。结果被推到堆栈上但从未使用过。
  • 直接连接(以及链接的StringBuilder)重用已经在堆栈中的str.append的结果
  • 两者(strstr.append的结果)指向相同的堆位置,但我不知道这是否可以由编译器派生。似乎当前的优化级别无法对其进行优化。