gpt4 book ai didi

java - 为什么流畅的实现比非流畅的实现慢?

转载 作者:行者123 更新时间:2023-12-03 03:00:56 25 4
gpt4 key购买 nike

我编写了一些测试代码来比较使用 StringBuilderappend() 方法的速度连续八次为 fluent interface反对在 8 行中单独调用它。

流利:

StringBuilder s = new StringBuilder();
s.append(x)
.append(y)
.append(z); //etc

不流利:

StringBuilder s = new StringBuilder();
s.append(x)
s.append(y)
s.append(z); //etc

每个方法都被调用了 1000 万次。每个 block 之间都会调用 GC。执行版本的顺序颠倒过来,得到相同的结果。

我的测试表明,流畅版本的代码大约慢了 10%(仅供引用,测试代码对于匹配但不可预测的附加是公平的,并且我给了 JVM 预热时间等)。

这是一个惊喜,因为流畅的代码只有一行。

为什么非流畅代码会更快?

最佳答案

我怀疑这是某些 Java 版本的功能。

如果我运行以下命令

public class Main {

public static final int RUNS = 100000000;

static final ThreadLocal<StringBuilder> STRING_BUILDER_THREAD_LOCAL = new ThreadLocal<StringBuilder>() {
@Override
protected StringBuilder initialValue() {
return new StringBuilder();
}
};

public static final StringBuilder myStringBuilder() {
StringBuilder sb = STRING_BUILDER_THREAD_LOCAL.get();
sb.setLength(0);
return sb;
}

public static long testSeparate(String x, String y, String z) {
long start = System.nanoTime();
for (int i = 0; i < RUNS; i++) {
StringBuilder s = myStringBuilder();
s.append(x)
.append(y)
.append(z);
dontOptimiseAway = s.toString();
}
long time = System.nanoTime() - start;
return time;
}

public static long testChained(String x, String y, String z) {
long start = System.nanoTime();
for (int i = 0; i < RUNS; i++) {
StringBuilder s = myStringBuilder();
s.append(x);
s.append(y);
s.append(z);
dontOptimiseAway = s.toString();
}
long time = System.nanoTime() - start;
return time;
}

static String dontOptimiseAway = null;

public static void main(String... args) {
for (int i = 0; i < 10; i++) {
long time1 = testSeparate("x", "y", "z");
long time2 = testChained("x", "y", "z");
System.out.printf("Average time separate %.1f ns, chained %.1f ns%n",
(double) time1 / RUNS, (double) time2 / RUNS);
}
}
}

使用 Java 7 更新 4

Average time separate 49.8 ns, chained 49.0 ns
Average time separate 50.7 ns, chained 49.3 ns
Average time separate 46.9 ns, chained 46.5 ns
Average time separate 46.6 ns, chained 46.4 ns
Average time separate 46.6 ns, chained 46.6 ns
Average time separate 47.6 ns, chained 47.3 ns
Average time separate 46.7 ns, chained 47.2 ns
Average time separate 46.7 ns, chained 47.0 ns
Average time separate 46.0 ns, chained 46.6 ns
Average time separate 46.7 ns, chained 46.3 ns

使用 Java 7 更新 10

Average time separate 50.4 ns, chained 50.0 ns
Average time separate 50.1 ns, chained 50.1 ns
Average time separate 45.9 ns, chained 46.5 ns
Average time separate 46.6 ns, chained 46.7 ns
Average time separate 46.3 ns, chained 46.4 ns
Average time separate 46.7 ns, chained 46.5 ns
Average time separate 46.2 ns, chained 46.4 ns
Average time separate 46.6 ns, chained 46.0 ns
Average time separate 46.4 ns, chained 46.2 ns
Average time separate 45.9 ns, chained 46.2 ns

最初看起来可能存在轻微偏差,但如果您运行 update 10,则随着时间的推移不会出现明显偏差。

关于java - 为什么流畅的实现比非流畅的实现慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14100786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com