gpt4 book ai didi

java - 为什么在 Java 中第一次调用方法时会有运行时开销?

转载 作者:行者123 更新时间:2023-11-30 10:35:44 24 4
gpt4 key购买 nike

我正在测量我的代码的执行时间,并在第一次调用方法(从 main 方法)时发现了一些奇怪的行为。这是我的代码,请看看这个

public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
int iNum = input.nextInt();
long lStartTime = System.nanoTime();

// ***********(First call in main) Calling isPrime *************

lStartTime = System.nanoTime();
printResult(isPrime(iNum));
System.out.println("Time Consumed in first call-: "
+ (System.nanoTime() - lStartTime));

// ***********(Second call in main) Calling isPrime *************
lStartTime = System.nanoTime();
printResult(isPrime(iNum));
System.out.println("Time Consumed in second call-: "
+ (System.nanoTime() - lStartTime));
}
}

private static boolean isPrime(int iNum) {
boolean bResult = true;

if (iNum <= 1 || iNum != 2 && iNum % 2 == 0) {
bResult = false;
} else {
double iSqrt = Math.sqrt((double) iNum);
for (int i = 3; i < iSqrt; i += 2) {
if (iNum % i == 0) {
bResult = false;
break;
}
}
}
return bResult;
}

private static void printResult(boolean bResult) {
if (bResult)
System.out.println("\nIt's prime number.");
else
System.out.println("\nIt's not prime number.");
}

输入

5

输出

It's prime number.
Time Consumed in first call-: 484073

It's prime number.
Time Consumed in second call-: 40710

描述

上面我只描述了一个输入和输出的测试用例。但是,第一个方法调用和第二个方法调用之间的执行时间始终存在差异。

我也试过两种以上的方法调用方式类似,发现除了一个之外,其他调用之间并没有那么大的区别。除了第一个方法调用是 484073ns 之外,其余调用的正确执行时间约为 40710ns(此执行时间在您的系统上可能不同)。我很容易看出在第一个方法调用中有 484073 - 40710 = 443363ns(大约)的时间开销,但为什么会这样?根本原因是什么?

最佳答案

Java Runtime Environment 有多种实现,因此并非每个实现都像 Oracle 的(以及以前的 Sun 的)一样。

开始说,在大多数当前实现中,方法的初始调用涉及验证和 Java 字节码的第一次编译。因此,该方法的后续调用会更快。但是,Java 也使用 JIT。维基百科提供了关于 Just-in-time compilation 的条目哪些笔记

JIT causes a slight delay to a noticeable delay in initial execution of an application, due to the time taken to load and compile the bytecode.

然后,继续说,

The application code is initially interpreted, but the JVM monitors which sequences of bytecode are frequently executed and translates them to machine code for direct execution on the hardware.

关于java - 为什么在 Java 中第一次调用方法时会有运行时开销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40966836/

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