Ashish Kumar
Ashish Kumar

Reputation: 926

Why is there run time overhead on the first time a method is called in Java?

I was measuring execution time of my code and found some weird behavior when making the first call to a method (from the main method). Here is my code, please have a look at this

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.");
}

Input

5

Output

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

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

Description

I have depicted only one test case of input and output above. But, there is always a difference in execution time between the first method invocation and the second one.

I have also tried more than two methods calls in the similar way and found that there is not such a huge difference between the other calls except one. I'm getting right execution time around 40710ns (this execution time could be different on your system) for the rest of calls except first method call which is 484073ns. Easily I can see that there is time overhead of 484073 - 40710 = 443363ns (approx) in first method call, but why is it happening? What is the root cause?

Upvotes: 2

Views: 653

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

There are multiple implementations of the Java Runtime Environment, so not every implementation may behave like Oracle's (and previously Sun's).

That begin said the initial invocation of a method, in most current implementations, involves validation and a first pass compilation of the Java bytecode. Thus, subsequent invocations of the method, are faster. However, Java also uses a JIT. Wikipedia provides an entry on Just-in-time compilation which notes

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.

And, goes on to say,

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.

Upvotes: 2

Related Questions