简单的字符串concat示例在Java 7中很快,在Java 8中很慢

时间:2014-04-15 17:55:06

标签: java performance

我有一个小类,它演示了连接字符串的不同方法的一些时序特性(StringBuilder与普通字符串连接)。无论每个策略的相对时间如何,我都注意到Java 6的特定性能行为,然后是Java 7,现在是Java 8。

我首先使用Java 6编写它,我对Java 7所看到的大量改进印象深刻。我曾认为Java 8会表现出类似的性能。但是,当我在Win7 / 64位笔记本电脑上运行时,我发现Java 8号码甚至比我的Java 6号码慢。

我完全有可能看到Java 6和7的问题,其中JVM正在删除我在运行时测试的代码(微基准测试的常见障碍),但我只是#&# 39;不知道。

我将显示三次运行的穿孔号码,然后我将包含源代码。请注意,测试有一个很大的热身传递,希望在定时运行之前,所有类加载和JIT工作都会完成。

另请注意,Java 6和Java 8中的相对时间是"一致"。例如,我希望测试2和3是两个最快,2有时快于3,反之亦然。 Java 7时序远低于此,并且大多数时间差不多,因此我可能会遇到Java 7 JIT过多代码的问题。

Java 6:
stringBuilder0:499
stringBuilder1:260
stringBuilder2:262
stringBuilder3:180
stringBuilder4:184
stringBuilder5:210
stringBuilder6:286
stringBuilder7:287
stringBuilder8:203
stringBuilder9:248

Java 7:
stringBuilder0:66
stringBuilder1:269
stringBuilder2:57
stringBuilder3:57
stringBuilder4:57
stringBuilder5:57
stringBuilder6:299
stringBuilder7:57
stringBuilder8:57
stringBuilder9:57

Java 8:
stringBuilder0:714
stringBuilder1:367
stringBuilder2:368
stringBuilder3:279
stringBuilder4:271
stringBuilder5:318
stringBuilder6:415
stringBuilder7:406
stringBuilder8:308
stringBuilder9:349

以下是生成此数据的两个类。

StringBuilderTests:

package timings;

public class StringBuilderTests {
public static void main(String[] args) {
    StringBuilderTests  stringBuilderTests  = new StringBuilderTests(args);
    stringBuilderTests.go();
}

public StringBuilderTests(String[] args) {
}

private void go() {
    final String  str1    = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
    final String  str2    = "abcdefghiabcdefghiabcdefghiabcdefghiabcdefghi";
    final String  str3    = "xxxxxxxxxxxxxxxx";
    final String  str4    = "yyyyyyyyyyyyyyyy";
    final String  str5    = "zzzzzzzzzzzzzzzz";

    System.out.println("Running warmup pass.");

    for (int ctr = 0; ctr < 20000; ++ ctr) {
    testStringBuilder0(str1, str2, str3, str4, str5);
    testStringBuilder1(str1, str2, str3, str4, str5);
    testStringBuilder2(str1, str2, str3, str4, str5);
    testStringBuilder3(str1, str2, str3, str4, str5);
    testStringBuilder4(str1, str2, str3, str4, str5);
    testStringBuilder5(str1, str2, str3, str4, str5);
    testStringBuilder6(str1, str2, str3, str4, str5);
    testStringBuilder7(str1, str2, str3, str4, str5);
    testStringBuilder8(str1, str2, str3, str4, str5);
    testStringBuilder9(str1, str2, str3, str4, str5);
    }

    int     iters   = 10000000;

    System.out.println("Starting timing runs.");

    new TimingContainer(iters, "stringBuilder0", new TimingTest() {
    public void run() {
        testStringBuilder0(str1, str2, str3, str4, str5);
    }
    }).run();

    new TimingContainer(iters, "stringBuilder1", new TimingTest() {
    public void run() {
        testStringBuilder1(str1, str2, str3, str4, str5);
    }
    }).run();

    new TimingContainer(iters, "stringBuilder2", new TimingTest() {
    public void run() {
        testStringBuilder2(str1, str2, str3, str4, str5);
    }
    }).run();

    new TimingContainer(iters, "stringBuilder3", new TimingTest() {
    public void run() {
        testStringBuilder3(str1, str2, str3, str4, str5);
    }
    }).run();

    new TimingContainer(iters, "stringBuilder4", new TimingTest() {
    public void run() {
        testStringBuilder4(str1, str2, str3, str4, str5);
    }
    }).run();
    new TimingContainer(iters, "stringBuilder5", new TimingTest() {
    public void run() {
        testStringBuilder5(str1, str2, str3, str4, str5);
    }
    }).run();
    new TimingContainer(iters, "stringBuilder6", new TimingTest() {
    public void run() {
        testStringBuilder6(str1, str2, str3, str4, str5);
    }
    }).run();
    new TimingContainer(iters, "stringBuilder7", new TimingTest() {
    public void run() {
        testStringBuilder7(str1, str2, str3, str4, str5);
    }
    }).run();
    new TimingContainer(iters, "stringBuilder8", new TimingTest() {
    public void run() {
        testStringBuilder8(str1, str2, str3, str4, str5);
    }
    }).run();
    new TimingContainer(iters, "stringBuilder9", new TimingTest() {
    public void run() {
        testStringBuilder9(str1, str2, str3, str4, str5);
    }
    }).run();
}

private void useit(String str) {
    System.out.print(str.charAt(0));
}

public static String testStringBuilder0(String str1, String str2, String str3, String str4, String str5) {
    String  result  = null;
    result  = result + str1;
    result  = result + str2;
    result  = result + str3;
    result  = result + str4;
    result  = result + str5;
    return result;
}

public static String testStringBuilder1(String str1, String str2, String str3, String str4, String str5) {
    StringBuilder   sb  = new StringBuilder();
    sb.append(str1);
    sb.append("-");
    sb.append(str2);
    sb.append("-");
    sb.append(str3);
    sb.append("-");
    sb.append(str4);
    sb.append("-");
    sb.append(str5);
    return sb.toString();
}

public static String testStringBuilder2(String str1, String str2, String str3, String str4, String str5) {
    return new StringBuilder().
        append(str1).append("-").append(str2).append("-").append(str3).append("-").append(str4).append("-").append(str5).
        toString();
}

public static String testStringBuilder3(String str1, String str2, String str3, String str4, String str5) {
    return new StringBuilder(str1).append("-").append(str2).append("-").append(str3).append("-").append(str4).append("-").append(str5).toString();
}

public static String testStringBuilder4(String str1, String str2, String str3, String str4, String str5) {
    return str1 + "-" + str2 + "-" + str3 + "-" + str4 + "-" + str5;
}

public static String testStringBuilder5(String str1, String str2, String str3, String str4, String str5) {
    return new StringBuilder(160).
        append(str1).append("-").append(str2).append("-").append(str3).append("-").append(str4).append("-").append(str5).
        toString();
}

public static String testStringBuilder6(String str1, String str2, String str3, String str4, String str5) {
    StringBuffer   sb  = new StringBuffer();
    sb.append(str1);
    sb.append("-");
    sb.append(str2);
    sb.append("-");
    sb.append(str3);
    sb.append("-");
    sb.append(str4);
    sb.append("-");
    sb.append(str5);
    return sb.toString();
}

public static String testStringBuilder7(String str1, String str2, String str3, String str4, String str5) {
    return new StringBuffer().
        append(str1).append("-").append(str2).append("-").append(str3).append("-").append(str4).append("-").append(str5).
        toString();
}

public static String testStringBuilder8(String str1, String str2, String str3, String str4, String str5) {
    return new StringBuffer(str1).append("-").append(str2).append("-").append(str3).append("-").append(str4).append("-").append(str5).toString();
}

public static String testStringBuilder9(String str1, String str2, String str3, String str4, String str5) {
    return new StringBuffer(160).
        append(str1).append("-").append(str2).append("-").append(str3).append("-").append(str4).append("-").append(str5).
        toString();
}
}

TimingContainer:

package timings;

public class TimingContainer {
private int         iterations;
private String      label;
private TimingTest  timingTest;

public TimingContainer(int iterations, String label, TimingTest timingTest) {
    this.iterations = iterations;
    this.label      = label;
    this.timingTest = timingTest;
}

public void run() {
    long startTime  = System.nanoTime();
    for (int ctr = 0; ctr < iterations; ++ ctr) {
    timingTest.randomize();
    timingTest.run();
    }
    long    endTime = System.nanoTime();
    long    totalns = (endTime - startTime);
    System.out.println(label + ":" + (totalns / iterations));
}
}

更新

TimingTest:

package timings;

public abstract class TimingTest {
public abstract void run();
public void randomize() {}
}

1 个答案:

答案 0 :(得分:1)

差异在于我将64位Java 7与32位Java 8进行了比较(Spring Tool Suite 3.5.0中的Eclipse显然不会使用64位Java 8,这就是我的原因。有32位Java 8发行版。

一旦我使用64位Java 8运行测试,我得到了更合理的结果。

此外,我了解了JMH,这对于编写和运行需要准确的微基准测试非常方便。