Java null 检查性能

May*_*day 1 java performance performance-testing

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

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

noFnCalls 经过时间:583

withFnCalls 已用时间:463

为什么会产生这样的情况呢?

eis*_*eis 5

您看到的结果可能是由于首先运行“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
Run Code Online (Sandbox Code Playgroud)

使用

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

如果我增加到1000000000L迭代。这是使用 Oracle 的 Java 9 64 位服务器 jvm(版本 9+181)完成的,在 Windows 10 上运行,机器配备 Intel i5-2600 cpu。

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

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