简体   繁体   中英

Java: Strange runtime behaviour in main

I experience a (for me) strange runtimebehaviour in the following code:

public class Main{

    private final static long ROUNDS = 1000000;
    private final static double INITIAL_NUMBER = 0.45781929d;
    private final static double DIFFERENCE = 0.1250120303d;


    public static void main(String[] args){

        doSomething();
        doSomething();
        doSomething();
    }


    private static void doSomething(){

        long begin, end;
        double numberToConvert, difference;


        numberToConvert = INITIAL_NUMBER;
        difference = DIFFERENCE;

        begin = System.currentTimeMillis();

        for(long i=0; i<ROUNDS; i++){

            String s = "" + numberToConvert;

            if(i % 2 == 0){
                numberToConvert += difference;
            }
            else{
                numberToConvert -= difference;
            }
        }

        end = System.currentTimeMillis();

        System.out.println("String appending conversion took " + (end - begin) + "ms.");
    }
}

I would expect the program to print out similiar runtimes each time. However, the output I get is always like this:

String appending conversion took 473ms.
String appending conversion took 362ms.
String appending conversion took 341ms.

The first call is about 30% slower than the calls afterwards. Most of the time, the second call is also slightly slower than the third call.

java/javac versions:

javac 1.7.0_09 java version "1.7.0_09" OpenJDK Runtime Environment (IcedTea7 2.3.3) (7u9-2.3.3-0ubuntu1~12.04.1) OpenJDK 64-Bit Server VM (build 23.2-b09, mixed mode)

So, my question: Why does this happen?

The Just-in-time (JIT) compiler is profiling your code on the fly and optimizing execution. The more often a piece of code is executed the better it's optimized.

See, for example, this question for more info: (How) does the Java JIT compiler optimize my code?

It is possible that other apps you have running are affecting the amount of memory you have allocated to the JVM on your machine. Try setting the same min and max memory to JVM when running the java command:

java -Xms512M -Xmx512M ...

I got fairly consistent intervals when trying to run it:

String appending conversion took 1153ms.
String appending conversion took 1095ms.
String appending conversion took 1081ms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM