gpt4 book ai didi

Java 空检查性能

转载 作者:行者123 更新时间:2023-11-29 08:28:00 25 4
gpt4 key购买 nike

我想知道在 java 中通过直接比较或使用 Objects.isNull() 方法检查对象是否为 null 是否有任何显着差异。

public class Test {

public final static Long ITERATIONS = 100000000L;

@Test
public void noFnCalls() {
balong startTime = System.currentTimeMillis();
Object x = new Object();
Long i;
for (i = 0L; i < ITERATIONS; i++) {
boolean t = x == null;
}
long estimatedTime = System.currentTimeMillis() - startTime;
System.out.println("noFnCalls ellapsed time: " + estimatedTime);
}

@Test
public void withFnCalls() {
long startTime = System.currentTimeMillis();
Object x = new Object();
Long i;
for (i = 0L; i < ITERATIONS; i++) {
boolean t = Objects.isNull(x);
}
long estimatedTime = System.currentTimeMillis() - startTime;
System.out.println("withFnCalls ellapsed time: " + estimatedTime);
}
}

令人惊讶的是,至少对我而言,完成“noFnCalls”总是需要更多时间。我期待的结果恰恰相反,因为它会导致使用堆栈进行方法调用。

这是输出:(显然,每次都会更改,但总是“noFnCalls”更高)

noFnCalls ellapsed time: 583

withFnCalls ellapsed time: 463

为什么会产生这个?

最佳答案

您看到的结果可能是由于首先运行“noFnCalls”,而没有在测试和测量之前引入适当的预热。

我明白了:

withFnCalls ellapsed time: 444
noFnCalls ellapsed time: 471
withFnCalls ellapsed time: 334
noFnCalls ellapsed time: 331
withFnCalls ellapsed time: 330
noFnCalls ellapsed time: 325
withFnCalls ellapsed time: 331
noFnCalls ellapsed time: 326
withFnCalls ellapsed time: 326
noFnCalls ellapsed time: 328

使用

import java.util.Objects;

public class Test {

public final static Long ITERATIONS = 100000000L;

public static void main(String args[]) {
withFnCalls();
noFnCalls();
withFnCalls();
noFnCalls();
withFnCalls();
noFnCalls();
withFnCalls();
noFnCalls();
withFnCalls();
noFnCalls();
}
public static void noFnCalls() {
long startTime = System.currentTimeMillis();
Object x = new Object();
Long i;
for (i = 0L; i < ITERATIONS; i++) {
boolean t = x == null;
}
long estimatedTime = System.currentTimeMillis() - startTime;
System.out.println("noFnCalls ellapsed time: " + estimatedTime);
}

public static void withFnCalls() {
long startTime = System.currentTimeMillis();
Object x = new Object();
Long i;
for (i = 0L; i < ITERATIONS; i++) {
boolean t = Objects.isNull(x);
}
long estimatedTime = System.currentTimeMillis() - startTime;
System.out.println("withFnCalls ellapsed time: " + estimatedTime);
}
}

withFnCalls ellapsed time: 3618
noFnCalls ellapsed time: 3361
withFnCalls ellapsed time: 3445
noFnCalls ellapsed time: 3278
withFnCalls ellapsed time: 3350
noFnCalls ellapsed time: 3292
withFnCalls ellapsed time: 3309
noFnCalls ellapsed time: 3262
withFnCalls ellapsed time: 3293
noFnCalls ellapsed time: 3261

如果我增加到 1000000000L 次迭代。这是使用 Java 9 64 位服务器 jvm,构建 9+181,由 Oracle 完成的,在 Windows 10 上运行,机器具有 Intel i5-2600 cpu。

正如其他人所说,微基准测试很难并且很多不同的因素都会影响结果。您不应该通过此类测试得出结论。这种测试并不能说明什么 - 任何差异都容易在彼此如此接近的噪声测量代码中丢失。

关于 java 微基准测试的强制性推荐线程:How do I write a correct micro-benchmark in Java? .

关于Java 空检查性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50755059/

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